【问题标题】:Permanent redirect from http to https page从 http 到 https 页面的永久重定向
【发布时间】:2017-10-30 23:22:56
【问题描述】:

当我尝试使用 .htaccess 创建从 HTTP 页面重定向到 HTTPS 页面(仅针对特定页面)的规则时,我收到了循环重定向。我哪里错了?

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^(/doctor) [NC, OR]
RewriteCond %{REQUEST_URI} ^(/client)
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^(/doctor) [NC, OR]
RewriteCond %{REQUEST_URI} !^(/client)
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L]

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    强制 https 用于特定文件夹

    RewriteEngine On 
    RewriteCond %{SERVER_PORT} 80 
    RewriteCond %{REQUEST_URI} somefolder 
    RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]
    

    供整个网站使用

    RewriteEngine On 
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
    

    对于您可以使用的特定页面

    RewriteEngine on
    RewriteCond %{SERVER_PORT} !^443$
    RewriteCond %{REQUEST_URI} ^/doctor/?.*$
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]
    

    检查服务器端口是否不同于 443(安全连接的标准),以确保我们只重定向非安全连接

    将页面secure.php重定向到安全域,发送301响应状态,附加所有查询字符串并标记为最后一条规则,因此任何低于此的规则都不会被解析(因为这是重定向,我们不想采取任何其他措施)

    这是我找到的不使用 .htaccess 的解决方案here

        <?php
    
    //assuming you site structure like www.domain.com/p=doctor
    
        $redirectlist = array('doctor','nurse','anyother');
    
        if (in_array($_GET['p'], $redirectlist) && strtolower($_SERVER['HTTPS']) != 'on') {
            exit(header("location: https://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}"));
        }
    
    ?>
    

    【讨论】:

    • 我明白了,但是当我想显示所有页面时遇到问题,这些页面不位于 /doctor 和 /client 下而没有 https。
    • 不要使用 SERVER_PORT 变量,而是使用 HTTPS,这样如果监听端口发生变化(或者你监听多个端口),重定向仍然有效。
    • 使用 [NE] 标志,这样 URL 中的保留字符就不会被重新编码。
    【解决方案2】:

    我找到了问题的答案:

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{HTTPS} !on
    RewriteCond %{REQUEST_URI} ^(/(client/|doctor/))
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
    
    RewriteCond %{HTTPS} on
    RewriteCond %{REQUEST_URI} !^(/(client/|doctor/))
    RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
    

    【讨论】:

      【解决方案3】:

      试试这个:

      Options +FollowSymLinks
      RewriteEngine On
      
      RewriteCond %{HTTPS} !on
      RewriteCond %{REQUEST_URI} ^/doctor/?.*$
      RewriteCond %{REQUEST_URI} ^/client/?.*$
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      

      如果 https 未打开,并且您正在访问 /doctor 或 /client(或这两者的任何子文件夹),它应该转发到 HTTPS。

      【讨论】:

      • 在什么情况下不起作用?你是用这一套替换你的两套重写规则吗?
      • 它根本不起作用。我已将此文本复制到我的 .htaccess 中,但我没有重定向到 https 页面。
      • 好的,确保您仍然拥有 RewriteEngine on 语句,并且您已经删除了现有的 HTTPS 规则。您是直接访问 /doctor 还是该文件夹中的页面?
      • @Kazar:首先 - 非常感谢您的帮助。是的,我再次复制了 .htaccess 文件中的所有文本。但是,我没有发现任何变化。它不起作用。我可以通过下一个 URL 访问该页面,例如:domainname.org/doctor/home.php
      • 您的主服务器配置是否设置为允许在 .htaccess 文件中重写规则?
      【解决方案4】:

      与 Kazar 的回答类似,但更简洁。

      Options +FollowSymLinks
      RewriteEngine On
      
      RewriteCond %{HTTPS} !on
      RewriteRule !/(doctor|client)(/.*)?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
      

      【讨论】:

      • 它更简洁,但是如果您需要添加新页面以强制使用 HTTPS,那么您将不得不修改该正则表达式,而不是添加 RewriteCond。您开始获得 20 个左右的安全页面,而该正则表达式将会变得很长。
      猜你喜欢
      • 1970-01-01
      • 2016-09-06
      • 2021-10-09
      • 1970-01-01
      • 2017-06-23
      • 2016-12-12
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      相关资源
      最近更新 更多