【发布时间】:2021-11-29 04:04:36
【问题描述】:
我的 .htacces 代码有问题,因为它会产生 500 内部服务器错误而不是 404 错误,就像它应该的那样。
500 内部错误仅在我尝试在实际上是文件的目录中打开页面时引起。例如,在我网站的根目录下,有一个文件biography.php。 https://example.com/biography.php 重定向到 https://example.com/biography/。
但是当我尝试打开不存在的页面 https://example.com/biography/test/ 时,它显示的是 500 内部服务器错误,而不是预期的 404 错误。
这是我的 .htacces 代码。最后 6 行似乎是导致问题的原因,因为没有它们,将显示预期的 404 错误,而不是 500 内部服务器错误。但是没有它们,https://example.com/biography.php 不会重定向到 https://example.com/biography/...
也许有一个重定向循环或类似的东西?我只是从互联网上复制代码,所以我无法自己解决问题。
Options -Indexes
AddDefaultCharset UTF-8
ServerSignature Off
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !(^$|\.[a-zA-Z0-9]{1,5}|/)$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^((/?[^/]+){1,2})/$ $1.php [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
感谢您的帮助!我希望你能理解我的问题!我很感激! :)
【问题讨论】:
标签: .htaccess redirect http-status-code-404 http-status-code-500