2015-01-20 80 views
1

試圖讓所有的請求mydomain.com/downloads/*運行通過download.php。因爲這也可能是mydomain.com/some/path/downloads/*我需要從https://stackoverflow.com/a/21063276獲取的動態RewriteBase,但是在RewriteCond模式中使用創建的Environment變量時,它不起作用,並且日誌顯示原始%{ENV :BASE}而不是展開變量。RewriteCond模式不擴展環境變量

的.htaccess中domain.com/some/path:

RewriteEngine On 
RewriteBase/

# Dynamic RewriteBase from https://stackoverflow.com/a/21063276 
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$ 
RewriteRule ^(.*)$ - [E=BASE:%1] 

RewriteCond %{REQUEST_URI} ^%{ENV:BASE}/?download/(.*) [NC] 
RewriteRule .* %{ENV:BASE}/download.php?file=%1&%{QUERY_STRING} [L] 

重寫日誌:

(3) [perdir /var/www/123/some/path/] strip per-dir prefix: /var/www/123/some/path/download/file/225 -> download/file/225 
(3) [perdir /var/www/123/some/path/] applying pattern '^(.*)$' to uri 'download/file/225' 
(4) [perdir /var/www/123/some/path/] RewriteCond: input='/some/path/download/file/225::download/file/225' pattern='^(.*?/)(.*)::\\2$' => matched 
(5) setting env variable 'BASE' to '/some/path/' 
(3) [perdir /var/www/123/some/path/] add path info postfix: /var/www/123/some/path/download -> /var/www/123/some/path/download/file/225 
(3) [perdir /var/www/123/some/path/] strip per-dir prefix: /var/www/123/some/path/download/file/225 -> download/file/225 
(3) [perdir /var/www/123/some/path/] applying pattern '^(.*)$' to uri 'download/file/225' 
(4) [perdir /var/www/123/some/path/] RewriteCond: input='/var/www/123/some/path/download' pattern='!-f' => matched 
(4) [perdir /var/www/123/some/path/] RewriteCond: input='/some/path/download/file/225' pattern='^%{ENV:BASE}/?download/' [NC] => not-matched 
(1) [perdir /var/www/123/some/path/] pass through /var/www/123/some/path/download 

BASE是正確設置在第4行,但是當在的RewriteCond檢查它沒有展開第9行。如果它正在擴展,它應該可以正常工作,我可以告訴。

有誰知道什麼是錯的,以及如何讓它正常工作?

Server版本:阿帕奇/ 2.2.15(Unix的)

回答

2

通過匹配模式在RewriteRule刪除RewriteBase線和解決您的第二個規則:

Options -MultiViews 
RewriteEngine On 

# Dynamic RewriteBase from http://stackoverflow.com/a/21063276 
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$ 
RewriteRule ^(.*)$ - [E=BASE:%1] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^download/(.+) %{ENV:BASE}/download.php?file=$1 [L,QSA] 
+1

謝謝,並獲得成功,沒想到關於規則中的匹配。儘管如此,您應該將規則中的%1修復爲$ 1,否則文件參數對我來說是空的。 – Phoenix 2015-01-20 22:12:10

+0

啊,沒錯,在修改規則時跳過了它。很高興它的工作。 – anubhava 2015-01-20 22:18:02