htaccess将所有请求重定向到index.php,子文件夹除外

时间:2012-12-20 01:39:59

标签: apache .htaccess mod-rewrite url-rewriting apache2

我在我的域的根目录下安装了CMS,其中包含以下htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

这需要友好的URL来将所有内容重定向到index.php。我的问题是我有一个子文件夹,其中有另一个cms。我需要将子文件夹的请求重定向到正确的文件夹而不是index.php。

我尝试了以下但它似乎无法正常工作

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(sub-folder)
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

有人可以帮忙吗?

由于

1 个答案:

答案 0 :(得分:16)

如果请求uri与子文件夹匹配,您应该正确验证

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/subfolder
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

在任何情况下,前两个重写意味着:如果请求文件名不是文件或目录。这意味着,如果您引用的文件位于子文件夹中,子文件夹位于docroot中,则无需执行任何操作。

如果仍然无效,启用日志可以帮助您:

RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

避免此登录生产

对Level使用较高的值会大大减慢Apache服务器的速度!仅在大于2的级别使用重写日志文件进行调试!