【问题标题】:.htaccess redirect https to http except one page along with www to non-www.htaccess 将 https 重定向到 http 除了一页以及 www 到非 www
【发布时间】:2014-03-26 06:09:07
【问题描述】:

在我的一个域上,我正在使用 htaccess

RewriteCond %{SERVER_PORT} ^443$  [OR]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L] 

将所有 https 重定向到 http 以及将 www 重定向到非 www 域。

现在,我希望 secure.php 页面使用 https 协议而不是 http 协议,同时将 www 重定向到非 www。

我可以使用下面的行来实现这个...

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]

RewriteCond %{HTTPS} off
RewriteCond %{SCRIPT_FILENAME} \/secure\.php [NC]
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{SCRIPT_FILENAME} !\/secure\.php [NC]
RewriteRule (.*) http://%{HTTP_HOST}/$1 [R=301,L] 

在我的第一个 htaccess 中,如果用户访问 https://www.example.com,然后在单个重定向中重定向到 http://example.com

修改安全secure.php页面后,在第二个htaccess中考虑用户访问http://www.example.com/secure.php的情况->重定向到http://example.com/secure.php->重定向到https://example.com/secure.php 在这里我得到了两个重定向。

请建议我如何使用 [OR] 像在我的第一个 htaccess 中那样使用我的标准,所以在所有情况下它最多使用一个重定向来到达最终页面。

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    试试:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      • 2018-11-12
      • 2020-05-27
      相关资源
      最近更新 更多