永久重定向从http到https页面

mis*_*sho 5 .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]
Run Code Online (Sandbox Code Playgroud)

ayu*_*ush 15

强制https用于特定文件夹使用

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

整个网站使用

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

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

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

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

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

这是一个没有使用.htaccess的解决方案,我在这里找到

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

?>
Run Code Online (Sandbox Code Playgroud)


mis*_*sho 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}
Run Code Online (Sandbox Code Playgroud)