apache - htaccess 将所有请求重定向到index.php(子文件夹除外)

标签 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]

有人可以帮忙吗?

谢谢

最佳答案

您应该正确验证请求 uri 是否与子文件夹匹配

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

无论如何,前两个rewritecond的意思是:如果请求文件名不是文件或目录。这意味着,如果您引用的文件位于子文件夹中,并且子文件夹位于您的文档根目录中,则您无需执行任何操作。

如果仍然不起作用,启用日志可以帮助您:

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

避免在生产环境中登录

使用较高的 Level 值会显着降低 Apache 服务器的速度!使用级别大于2的重写日志文件仅用于调试!

关于apache - htaccess 将所有请求重定向到index.php(子文件夹除外),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13964027/

相关文章:

apache - Chromecast 是否附带允许简单网络服务器身份验证/验证的证书包?

java - 如何在 tomcat 6 中创建 localhost_access_log 文件

apache - .htaccess 在多个环境中

php - LAMP 服务器 - 一个命令?

php - 通过php和bash执行服务命令

.htaccess - Application.cpp :249: can't acces file 中的 SoftException

regex - 在 htaccess 中用连字符替换 %20

javascript - 使用重写规则后未加载 CSS 、 js 、 jpg png 帮助我

Apache Mod-重写入门?

http - .htaccess 重定向仅在浏览器警告后执行