永久重定向从http到https页面

时间:2011-02-08 12:30:15

标签: .htaccess

当我尝试使用.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]

4 个答案:

答案 0 :(得分:15)

强制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']}"));
    }

?>

答案 1 :(得分:4)

我找到了问题的答案:

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}

答案 2 :(得分:2)

试试这个:

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。

答案 3 :(得分:1)

与Kazar的答案相似,但更简洁。

Options +FollowSymLinks
RewriteEngine On

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