【问题标题】:Multiple redirects with 301 htaccess使用 301 htaccess 进行多次重定向
【发布时间】:2018-06-07 19:02:30
【问题描述】:

我是重定向和正则表达式的新手。我在实现重定向时遇到问题。这是示例。我想要

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]

【问题讨论】:

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


    【解决方案1】:

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

    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]
    

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

    【讨论】:

    • 谢谢!给定的示例有效。但是下面不起作用https://www.example.com/support/abc/index.php redirects to https://www.example.com/support/abc & then redirects to https://www.example.com/support/abc/
    • 你能不能请他;lp
    • 不。这也没有用。它添加了额外的斜杠而不是删除 index.php https://www.example.com/index.php 301 redirect to https://www.example.com/index.php/
    • 这会将https://www.example.com/support/abc/index.php 重定向到https://www.example.com/support/abc/,如果您看到其他任何内容,则可能是由于旧的浏览器缓存造成的。
    • 我已经发布了带有图像的答案。谢谢
    【解决方案2】:

    @anubhava 您的编辑显示附件 1

    我发布的问题中的现有代码如下

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

    最近通过添加斜杠的更新结果如下

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-15
      • 2015-08-15
      • 2014-02-03
      • 1970-01-01
      • 2015-02-06
      相关资源
      最近更新 更多