当我使用更新的Apache版本(2.4)时,重写规则无法工作



我有一个Centos 5 VPS,然后使用Apache 2.4转到Centos 7。我不知道Centos5系统上的Apache早期版本是什么,但我知道我的"重写"规则运行得很好。

然而,当我转到Apache2.4时,我的旧重写规则停止了工作:

旧规则:
RewriteRule ^(/[^.]*)$ /index.php?page=$1 [NC]
在错误日志中触发此错误:

AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

我尝试的新规则至少让我的www.domainname.com正常工作:
RewriteRule ^([^/]*)$ /index.php?page=$1 [NC]

Apache 2.4有什么不同?无论是在这里还是在谷歌上,我都找不到任何线索。

您需要添加一个RewriteCond,以防止对index.php的请求被重写。有两种方法:

1) 只有当请求不以现有物理文件为目标时才重写:

Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} !-f
RewriteRule ^/?([^/]*)$ /index.php?page=$1 [END]

2) 只有当请求没有明确针对/index.php位置时才重写:

Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php$
RewriteRule ^/?([^/]*)$ /index.php?page=$1 [END]

领先的^/?确保此模式将在实际http服务器主机配置中的.htaccess样式文件中工作。这是有道理的,因为:

一般提示:您应该始终倾向于将此类规则放置在http服务器主机配置中,而不是使用.htaccess样式的文件。众所周知,这些文件容易出错,很难调试,而且它们确实会降低服务器的速度。它们只是在您无法控制主机配置的情况下(阅读:非常便宜的托管服务提供商),或者您的应用程序依赖于编写自己的重写规则(这显然是一场安全噩梦)的最后一个选项。

最新更新