2016-01-20 105 views
1

我想將所有請求重定向到應該根據子域處理它們的perl文件。.htaccess將所有請求重定向到perl文件

我試圖用這個的.htaccess:

Options +ExecCGI 
AddHandler fcgid-script .pl 
RewriteEngine On 

RewriteCond %{HTTP_HOST} ^(.+?)\.example\.com$ 
RewriteRule ^(.*)$ /%1/main.pl/$1 [L] 

我所期望的是,如果我嘗試打開master.example.com,我的〜/ HTML根/主/ main.pl將得到執行,但是我得到一個「內部服務器錯誤」。

Apache的error_log中說

[Wed Jan 20 18:06:36 2016] [error] [client xxx.xxx.xxx.xxx] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace., referer: http://master.example.com/ 

如果我嘗試example.com/master/main.pl我的腳本執行後就好了參觀。

我在做什麼錯?

回答

0

我發現那種一種解決方法:

Options +ExecCGI 
AddHandler fcgid-script .pl 
RewriteEngine On 

RewriteCond %{ENV:REDIRECT_ENV} !^.+$ 
RewriteCond %{HTTP_HOST} ^(.+?)\.example\.com$ 
RewriteRule ^(.*)$ /%1/main.pl/$1 [L,E=ENV:%1] 

而不是不將請求重定向到/\1/main.pl我第一重定向設置環境變量。如果設置了該變量,那麼我不會再執行任何重定向,從而避開無限循環,否則會發生這種情況。

1

試試你的規則,這樣一來,

Options +ExecCGI 
AddHandler fcgid-script .pl 
RewriteEngine On 
RewriteCond %{HTTP_HOST} ^(.+?)\.example\.com$ 
RewriteCond %1::%{REQUEST_URI} !^(.*?)::/\1/? [NC] 
RewriteRule ^(.*)$ /%1/main.pl/$1 [L] 
+0

This Works。你能向我解釋你的線路是什麼嗎? – dave

+0

@dave問題是你需要確保你忽略你指向的子文件夾。在你的情況下,它是'動態',因爲它取決於子域。您不能在rewritecond的正則表達式中使用反向引用。因此,此規則通過匹配和分組:: ::之前的文本塊並使用「\ 1」引用進行比較來在左側字符串中使用backrefence「%1」。它基本上是'RewriteCond%{REQUEST_URI}!^/master [NC]'的一個靜態等價物' –

+0

master.example.com/master再次拋出錯誤。這次它說Forbidden ... – dave

相關問題