【发布时间】:2015-03-19 05:03:19
【问题描述】:
尝试获取对 mydomain.com/downloads/* 的所有请求以通过 download.php 运行。因为这也可能是 mydomain.com/some/path/downloads/* 我需要从 https://stackoverflow.com/a/21063276 获取的动态 RewriteBase 但是当在 RewriteCond 模式中使用创建的环境变量时它不起作用并且日志显示原始百分比{ENV:BASE} 而不是扩展变量。
.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 行中设置正确,但是在第 9 行中检查 RewriteCond 时它没有展开。如果它正在展开,据我所知它应该可以正常工作。
有谁知道出了什么问题以及如何让它正常工作?
服务器版本:Apache/2.2.15 (Unix)
【问题讨论】:
标签: apache .htaccess mod-rewrite