Apache清除URL代码导致内部服务器错误

ffx8fchx  于 2022-11-16  发布在  Apache
关注(0)|答案(1)|浏览(157)

我正在本地主机(MAMP)上的一个项目上设置一个404页面,在我的.htaccess文件中,我包含了以下代码:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) $1.php [NC,L]
</IfModule>

ErrorDocument 404 http://localhost:8888/project/public/404.php

404.php页面与项目根目录下的公共文件夹中的.htaccess文件位于同一级别:

project
— publicFolder
— privateFolder

问题

当我在<IfModule mod_rewrite.c>中包含代码时,当我输入404页面URL(即页面不存在)时,它会抛出错误。当我删除此<IfModule mod_rewrite.c>代码块时,问题就消失了。错误消息是:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at you@example.com to inform them of the time this error occurred, and the actions you performed just before this error.
More information about this error may be available in the server error log.

当我检查apache错误日志时,按照上面消息中的建议,我得到了以下信息:

[core:error] [pid 2575] [client ::1:51038] 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.

我怀疑该问题可能是与删除所有页面上的.php文件扩展名的行有关,即RewriteRule (.+) $1.php [NC,L]

我的问题

如何在<IfModule mod_rewrite.c>块中获取干净的URL代码,以防止在用户访问应该是404页面时引发内部服务器错误?
任何帮助都不胜感激。

mbyulnm0

mbyulnm01#

您的问题仅仅是您确实实现了一个无休止的重写循环。
试着使用带有附加条件的变体:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]

您需要了解,您使用的L标志确实终止了重写,* 但仅限于当前周期 *。开始一个 * 新 * 循环。直到请求稳定为止,因为重写的目标 * 再次 * 匹配您的条件 * 并且 * 规则的匹配模式 * 再次 * 在实现中重写请求,最终会产生一个类似/foo.php.php.php.php.php的请求,在每个重写周期都附加一个“.php”。
另一种方法是使用新的[END] ³标志代替旧的[L]标志。END标志 * 完全 * 终止重写过程,忽略任何其他规则。因此,您需要确保该规则确实是最后一个应该应用的规则。使用该标志,您将不需要额外的条件,因为完全终止显然可以防止无休止的重写循环。
一般来说,我建议不要使用分布式配置文件(.htaccess),而是在实际的http服务器的中央主机配置中实现这样的规则,并在那里使用绝对路径。
这通常可以防止很多混乱和副作用。
当然,这里可能还有其他问题,不能从你提出的问题中立即弄清楚。但以上是一个明显的问题。
始终使用新的匿名浏览器窗口进行测试,始终在浏览器中进行 * 深度 * 重新加载,以防止测试时客户端缓存效应。

相关问题