【问题标题】:Http with .html to https without html in one redirect一个重定向中带有 .html 的 Http 到没有 html 的 https
【发布时间】:2018-04-10 16:26:41
【问题描述】:

我正在尝试重写我们网站的 htaccess 文件。我对 htaccess 不太好,但我正在研究我们现有的 .htaccess 文件,现在我理解的是我们有 htaccess 指令将 http 重定向到 https。

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

另一个右下方的请求从具有 .html 扩展名的请求重定向到没有 .html。

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

我不确定,但 R=301 是否意味着重定向? 如果是这样,它是否在 rewritecond 中的每次匹配后进行重定向? 有没有办法在尝试重定向到两个规则之前创建另一个匹配两个条件的指令?

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    您可以对两个 301 重定向使用一个重定向规则:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \.html\s [NC,OR]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.+?)(?:\.html)?$ https://%{HTTP_HOST}/$1 [R=301,NC,NE,L]
    

    【讨论】:

      【解决方案2】:

      你可以使用:

      RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
      RewriteRule ^(.*)\.html$ https://%{HTTP_HOST}/$1 [R=301,L]
      
      RewriteCond %{HTTPS} !=on
      RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
      

      【讨论】:

      • 感谢帮助但仍有2个R=301
      • 是的,但处决的从来不止一个
      猜你喜欢
      • 2016-07-06
      • 2015-02-13
      • 2018-01-21
      • 2019-07-10
      • 2017-05-10
      • 1970-01-01
      • 1970-01-01
      • 2021-12-26
      • 2018-04-15
      相关资源
      最近更新 更多