【问题标题】:Too Many Redirects Error. When trying to redirect a url重定向错误太多。尝试重定向 url 时
【发布时间】:2019-07-03 05:11:34
【问题描述】:

我的 .htaccess 文件有这个代码:-

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

在搜索引擎优化期间,我的网站显示 2 个 URL 重复错误:- https://example.net/contact-us.php & https://example.net/contact-us 指向同一个资源。因此,我们需要重定向 URL。为此,我编写了以下代码:-

重定向 301 /contact-us.php https://example.net/contact-us/

问题是它显示了太多重定向错误。请帮助。提前致谢。

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    你应该有两条规则。 1 将无扩展名重定向到 php 文件,1 将 php 重定向到无扩展名(如果有人键入)。您不需要那个Redirect 301 规则。

    我的规则如下所示。

    替换这些规则

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
    RewriteCond %{HTTPS} !on
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    

    有了这个

    RewriteEngine On
    
    #redirect a direct request for the php file to no extension
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/([^\s]+)\.php
    RewriteRule ^/?(.*)\.php$ /$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^/?(.*)/?$ /$1.php [L]
    
    RewriteCond %{HTTPS} off
    RewriteRule ^/?(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    【讨论】:

    • 谢谢。它做得很好。我试图找出过去 2 天的错误。你解决了。所以,非常感谢。我还需要一个帮助。您能否指出一个资源,该资源向我解释如何使用 .haccess 以及如何编写其规则?
    • 只需使用 Apache 文档来了解语法以及 mod_rewrite 的工作原理。然后有时会进行一些试验和错误,但请研究其他 SO 答案以了解更多信息。
    猜你喜欢
    • 1970-01-01
    • 2021-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多