永久重定向从http到https页面

时间:2022-05-09 11:04:09

When I try to create rules for redirect from HTTP pages to HTTPS pages (only for specific pages) with .htaccess, I've received loop redirect. Where am I wrong?

当我尝试使用.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 个解决方案

#1


15  

to force https for a particular folder use

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

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

for entire site use

整个网站使用

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

for specific pages you could use

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

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

Check if server port is different from 443 (standard for secure connections), to ensure we are going to redirect only non-secured connections

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

Redirect the page secure.php to secure domain, sending 301 response status, appending all query string and marking is as last rule, so any rules below this will not be parsed (since this is redirect, we don't want to take any other actions)

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

here is a solution without using .htaccess i found here

这是一个解决方案,不使用.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']}"));
    }

?>

#2


4  

I've found answer to my question:

我找到了回答我的问题:

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


2  

Try this:

尝试这个:

Options +FollowSymLinks
RewriteEngine On

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

If https is not on, and you are accessing /doctor or /client (or any sub-folders of those two), it should forward to HTTPS.

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

#4


1  

Similar to Kazar's answer, but more concise.

类似于Kazar的答案,但更简洁。

Options +FollowSymLinks
RewriteEngine On

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

#1


15  

to force https for a particular folder use

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

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

for entire site use

整个网站使用

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

for specific pages you could use

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

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

Check if server port is different from 443 (standard for secure connections), to ensure we are going to redirect only non-secured connections

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

Redirect the page secure.php to secure domain, sending 301 response status, appending all query string and marking is as last rule, so any rules below this will not be parsed (since this is redirect, we don't want to take any other actions)

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

here is a solution without using .htaccess i found here

这是一个解决方案,不使用.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']}"));
    }

?>

#2


4  

I've found answer to my question:

我找到了回答我的问题:

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


2  

Try this:

尝试这个:

Options +FollowSymLinks
RewriteEngine On

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

If https is not on, and you are accessing /doctor or /client (or any sub-folders of those two), it should forward to HTTPS.

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

#4


1  

Similar to Kazar's answer, but more concise.

类似于Kazar的答案,但更简洁。

Options +FollowSymLinks
RewriteEngine On

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