url 中的 '/' 重写 url 时出现 500 错误

500 Error when '/' in url with url rewriting

我的服务器上有这个 .htacess 可以重定向 example/test -> example/test.php.

ErrorDocument 404 http://example.net/404

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ .php [L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ .html [L]

它适用于基本 url 但是当我请求 "example/fakefolder/test" 它时 return 出现 500 错误。

我在日志中没有任何内容说我有 500 错误。

您看到的问题是 -f 检查与 %{REQUEST_FILENAME} 配对时有点像 "fuzzy" 检查。它包括路径信息样式匹配,这意味着如果你有一个文件 /script.php 并且请求是 /script/blahblah,条件 %{REQUEST_FILENAME}.php -f 实际上 returns true 因为它模糊匹配:/script.php/blahblah,它作为文件存在,通过路径信息样式 URL.

您需要明确说明您的路径并包括可能的 /.

ErrorDocument 404 http://rafflehaven.net/404

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/.php -f
RewriteRule ^(.*?)/?$ .php [L]

RewriteCond %{DOCUMENT_ROOT}/.html -f
RewriteRule ^(.*?)/?$ .html [L]