.htaccess 规则将所有请求重定向到单个 php 文件,2 个文件夹除外

.htaccess rule to redirect all requests to a single php file, except 2 folders

我需要一个好的“.htaccess”规则来将所有 apache 请求重定向到单个 php 文件,'files' 文件夹除外(对于 js、css 等)和一个 'static' 文件夹(用于静态 .html 文件,如错误页面)。

因为这是一个开发站点,所以它现在都在一个子文件夹中。这就是我使用 'SubFolder'.

的原因

我目前有:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(SubFolder/files|SubFolder/static)(/|$)
RewriteRule ^(.*)$ /SubFolder/index.php?path= [NC,L,QSA]

当我访问文件时出现“内部服务器错误”。当我检查 apache 日志时,它报告:“由于可能的配置错误,请求超出了 10 个内部重定向的限制。”

我怀疑这是因为我的规则反复出现,但我不明白为什么。你能帮忙吗?

I suspect this is due to my rule catching itself repeatedly, but I can't see why.

是的,您的规则也与 /SubFolder/index.php 匹配,因此会反复重写自己。您也可以为此包含一个例外。例如:

RewriteCond %{REQUEST_URI} !^/SubFolder/index\.php$
RewriteCond %{REQUEST_URI} !^/(SubFolder/files|SubFolder/static)(/|$)
RewriteRule (.*) /SubFolder/index.php?path= [L,QSA]

如果这是您唯一的规则,则不需要 RewriteBase 指令。 (此规则也不需要 NC 标志,因为 .* 无论如何都匹配大写和小写字母。)

如果需要,您可以组合条件

RewriteCond %{REQUEST_URI} !^/SubFolder/(index\.php|files|static)(/|$)
RewriteRule (.*) /SubFolder/index.php?path= [L,QSA]