RewriteCond的不区分大小写的文件检查

时间:2016-09-01 13:55:54

标签: apache .htaccess mod-rewrite case-insensitive

我有一个.htaccess文件,用于测试特定文件并运行PHP脚本(如果存在),并重定向到另一个网站(如果不存在的话)。

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/directory/$1 -f
RewriteRule ^(.+\.png)$ process.php?file=$1 [QSA]

RewriteRule ^(.+\.png)$ http://other.website.com/$1

我可以直接运行PHP脚本并进行重定向,但这种方式更快。但是,我需要使RewriteCond检查不区分大小写。我已经尝试设置所有标志并启用拼写检查,但无济于事。

RewriteEngine on
CheckSpelling on
RewriteCond %{DOCUMENT_ROOT}/directory/$1 -f [NC]
RewriteRule ^(.+\.png)$ process.php?file=$1 [QSA,NC]

RewriteRule ^(.+\.png)$ http://other.website.com/$1

在httpd.conf中启用了拼写模块,.htaccess文件可以修改所有可能的设置,但它仍然可以像以前一样工作,如果输入了带有不同文件名的文件,则不会重定向。

1 个答案:

答案 0 :(得分:1)

尝试在Apache配置中使用tolower映射。首先,在tolower(或httpd.conf或VHost配置)中调用apache2.confcreate a map

RewriteMap lc int:tolower

接下来,在条件检查中,将$1转换为小写:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/directory/${lc:$1} -f [NC]
RewriteRule ^(.+\.png)$ process.php?file=$1 [QSA,NC]

RewriteRule ^(.+\.png)$ http://other.website.com/$1

请注意,要使上述工作正常,您的文件应始终使用较低的字母。

相关问题