【问题标题】:RewriteCond Pattern not expanding Environment variablesRewriteCond 模式不扩展环境变量
【发布时间】: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


    【解决方案1】:

    删除RewriteBase 行并通过匹配RewriteRule 中的模式来修复您的第二条规则:

    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,否则文件参数对我来说是空的。
    • 嗯,没错,在修复规则时跳过了更改它。很高兴它成功了。
    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多