【问题标题】:htaccess RewriteRule to https doesn't work combined with other ruleshtaccess RewriteRule to https 不能与其他规则结合使用
【发布时间】:2016-02-18 00:21:13
【问题描述】:

我正在尝试在我的.htaccess 中设置一些规则,以便以特定方式工作:

  1. 特定页面重定向:

    RewriteRule "^web/site/mais$" https://www.main.com/front/for-you/insurance/ [R=301,L]
    
  2. 首页重定向

    RewriteRule ^$  https://www.forexample.com.br/for-you/ [R=301,L]
    RewriteRule ^/$  https://www.forexample.com.br/for-you/ [R=301,L]
    
  3. HTTP 到 HTTPS 并将 www 添加到其余部分(这是行不通的),我尝试了很多东西:

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteCond %{HTTPS} !^on$ [OR]
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{HTTP_HOST} ^www\.
    RewriteCond %{HTTPS} !^on$ [OR]
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

我尝试忘记 www 规则的其他方式(但也不起作用):

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

我发现合并的唯一方法是将一般的httpswww 规则写入httpd.conf 并将其他规则写入.htaccess,但是这样我得到两个或更多的301 重定向,生意不好搜索引擎优化。

【问题讨论】:

    标签: apache .htaccess redirect mod-rewrite


    【解决方案1】:

    您是否对所有目录或只有一些目录有相同的问题?

    因为如果您有 ProxyPass(如您所说),它可能会将重定向直接发送到 Tomcat,因此您可能需要在 ssl.conf 中指定规则以在离开 ssl.conf 之前读取该规则。尝试在ProxyPass之后将您写入的最后一个写入ssl.conf

    【讨论】:

    • 是的,我有图像、js 和 css 的代理通行证,将 www 的特定内容写入 ssl.conf 和 htaccess 并让 httpd.conf 仅使用图像的代理通行证, js 和 css, ir 为我工作。我使用的规则是:ssl.conf 在proxypass 和.htaccess 下:RewriteCond %{HTTP_HOST} !^www\. RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    【解决方案2】:

    如果你想将一些空间页面重定向到https,例如重定向:

    到 - https://www.example.com/page2.html

    您可以尝试以下方法:

     RewriteEngine on
     RewriteCond %{HTTP_HOST} !^www\. [OR]
     RewriteCond %{HTTPS} off
     RewriteRule ^(page|page2)\.html$ https://www.example.com/$1.html [L,R]
    

    如果你想将整个网站从http重定向到https,你可以使用以下:

     RewriteEngine on
    
    
     RewriteCond %{HTTP_HOST} !^www\. [OR]
     RewriteCond %{HTTPS} off
     RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R]
    

    【讨论】:

    • 它有效,但在许多情况下使用两个重定向。问题是我对主文件夹设置了 proxypass 规则,因此这些规则导致请求直接发送到 tomcat。我已经(或多或少地)解决了在 ssl.conf 上添加 www 规则的问题,让 httpd.conf 没有重定向,而 htaccess 中的其他(如你所说)则为空。这样,如果尝试在没有 www 的情况下从 https 重定向(我不知道解释是否清楚,但它对 SEO 乌鸦来说足够有效),我只会在每个特定但两个中获得一个 301 重定向)
    猜你喜欢
    • 2013-02-03
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 2018-02-02
    • 1970-01-01
    相关资源
    最近更新 更多