太多重定向错误。尝试重定向网址时

时间:2019-07-03 05:11:34

标签: apache .htaccess

我的.htaccess文件具有以下代码:-

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

在Seo期间,我的网站显示2个URL重复错误为:- https://example.net/contact-us.phphttps://example.net/contact-us指向相同的资源。因此,我们需要重定向URL。为此,我编写了以下代码:-

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

问题是它显示太多重定向错误。请帮助。预先感谢。

1 个答案:

答案 0 :(得分:1)

您应该有2条规则。 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]
相关问题