使用301 htaccess进行多次重定向

时间:2018-06-07 19:02:30

标签: .htaccess http-status-code-301

我是重新定向和正则表达式的新手。我遇到了重定向的问题。这是一个例子。我想要

http://www.example.com/support             redirect to
https://www.example.com/support/

但这是发生的事情

http://www.example.com/support             redirects to
https://www.example.com/support            redirects to
https://www.example.com/support/

下面是它的htaccess内容

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

# To remove double slash in the middle
RedirectMatch 301 ^/(.*)//(.*)$ https://www.example.com/$1/$2/

# redirect index.php to root
RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)/index\.php$ https://www.example.com/$1 [R=301] 

#index to root
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ https://www.example.com/$1 [R=301,L]

2 个答案:

答案 0 :(得分:0)

您可以使用这些规则来避免多次重定向:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

# To remove double slash anywhere
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /$0 [R=302,L,NE]

# redirect index.php to parent
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

确保使用新的浏览器进行测试。

答案 1 :(得分:0)

@anubhava您的编辑会显示附件1 Your code

我发布的问题中的现有代码如下所示 existing htaccess

但是,我正在寻找一个重定向到最终目的地..

通过添加尾部斜杠结果进行的最新更新如下 Recent edit - trailing slash