【问题标题】:.htaccess displays 500 internal server error instead of expected 404 error.htaccess 显示 500 内部服务器错误而不是预期的 404 错误
【发布时间】:2021-11-29 04:04:36
【问题描述】:

我的 .htacces 代码有问题,因为它会产生 500 内部服务器错误而不是 404 错误,就像它应该的那样。

500 内部错误仅在我尝试在实际上是文件的目录中打开页面时引起。例如,在我网站的根目录下,有一个文件biography.phphttps://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


    【解决方案1】:
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*?)/?$ $1.php [NC,L]
    

    重写循环(导致 500 错误)是由最后一条规则(3 个指令)引起的,因为检查 %{REQUEST_FILENAME}.php -f 的条件不一定检查您在 RewriteRule 中重写的同一文件 替换字符串(即$1.php)。

    当您请求/biography/test/ 时,您最终会检查/biography.php 是否存在(确实存在),但您最终会将请求重写为/biography/test.php(不存在) - 这会导致重写循环。

    查看my answer对ServerFault的以下问题的更详细解释:https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error

    最后一条规则应该这样写:

    # Rewrite to append the ".php" extension as required.
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*?)/?$ $1.php [L]
    

    这会在将.php 扩展名附加到相同的 URL 路径之前检查目标文件是否存在。

    不需要两个条件。并且这里不需要NC 标志。

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
    RewriteRule ^ %1 [R=301]
    

    但是,您重定向到删除 .php 扩展名也不是完全正确的。如果 .php “扩展名”出现在 URL 的查询字符串部分,这也会错误地删除它。而且您缺少L 标志。你也不需要两个条件

    请改用以下内容:

    # Remove ".php" extension from requested URL-path
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^(.+)\.php$ /$1 [R=301,L]
    

    总结

    # Remove ".php" extension from requested URL-path
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^(.+)\.php$ /$1 [R=301,L]
    
    # Rewrite to append the ".php" extension as required.
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*?)/?$ $1.php [L]
    

    【讨论】:

    • 非常感谢,现在可以使用了 :)
    猜你喜欢
    • 1970-01-01
    • 2012-06-28
    • 2012-04-06
    • 2012-07-29
    • 2016-09-22
    • 2017-04-05
    • 1970-01-01
    • 2013-02-06
    • 2015-06-28
    相关资源
    最近更新 更多