URL重写带有目录例外的毯子重定向

时间:2008-12-01 20:35:05

标签: apache mod-rewrite

我有一个网站,它使用通用的mod_rewrite规则将所有请求推送到index.php页面,但某些文件扩展名除外:

RewriteRule !\.(js|ico|gif|jpg|JPG|png|css|php|phtml|pdf|txt|xml)$ index.php

我需要做的是从该规则中排除某个目录(包括其中包含的任何文件或子目录) - 最佳解决方案是什么?

这是我的完整.htaccess文件,以防其他内容正在进行:

RewriteEngine ON
RewriteCond %{HTTP_HOST} !^www\..*
RewriteCond %{HTTP_HOST}   !^$
RewriteCond %{HTTP_HOST} ^([^.]*)\.(co\.uk)
RewriteRule ^.*$     http://www.%1.%2%{REQUEST_URI} [R=permanent,L]

AddHandler application/x-httpd-php .phtml

RewriteRule !\.(js|ico|gif|jpg|JPG|png|css|php|phtml|pdf|txt|xml)$ index.phtml

php_value display_errors "On"

2 个答案:

答案 0 :(得分:7)

在您引用的行之前,例如,对于名为“style”的目录,您需要:

RewriteRule  ^style/  -  [L]

连字符表示“无重定向”,“[L]”表示“最后一条规则”,因为不会继续尝试将URL与以下规则匹配。您可以根据需要添加尽可能多的这些行,但它们必须位于您在问题中提供的行之前。

答案 1 :(得分:0)

您可以与RewriteCond %{REQUEST_FILENAME} !-f核对任何与现有文件名不匹配的请求。