.htaccess 从http永久重定向到https页面

.htaccess 从http永久重定向到https页面,.htaccess,.htaccess,当我尝试使用.htaccess创建从HTTP页面重定向到HTTPS页面(仅针对特定页面)的规则时,我收到了循环重定向。我错在哪里 Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTPS} !on RewriteCond %{REQUEST_URI} ^(/doctor) [NC, OR] RewriteCond %{REQUEST_URI} ^(/client) RewriteRule (.*) https://%{HTTP_H

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

为特定文件夹使用强制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的解决方案

试试这个:

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。

我找到了问题的答案:

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}

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

Options +FollowSymLinks
RewriteEngine On

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

我明白了,但是当我想显示所有不在没有https的/doctor和/client下的页面时,我遇到了一个问题。不要使用SERVER\u PORT变量,而是使用https,这样,如果侦听端口更改(或者您侦听多个端口),重定向仍然有效。使用[NE]标记,以便URL中的保留字符不会重新编码。以何种方式不起作用?你要用这一套规则替换你的两套规则吗?它根本不起作用。我已在.htaccess中复制了此文本,但尚未重定向到https页面。好的,请确保您仍然拥有RewriteEngine on语句,并且已删除现有的https规则。您是直接访问/doctor还是该文件夹中的某个页面?@Kazar:首先,非常感谢您的帮助。是的,我再次复制了.htaccess文件中的所有文本。但是,我没有发现任何变化。它不起作用。我可以通过下一个URL访问该页面,例如:您的主服务器配置是否设置为允许在.htaccess文件中重写规则?这更简洁,但如果您需要添加新页面以强制使用HTTPS,则必须修改该正则表达式,而不是添加重写条件。你开始得到20个左右的安全页面,这个正则表达式会变得相当长。
Options +FollowSymLinks
RewriteEngine On

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