.htaccess从http重定向到页面的https

时间:2013-10-10 10:42:00

标签: regex apache .htaccess mod-rewrite https

我有/安全页面,我需要将打开此页面的用户重定向到https协议。但所有其他人只能使用http打开。我想用.htaccess

这样做
RewriteEngine on
RewriteRule ^secure(.+?) https://site.com/secure$1 [L,R=301]
RewriteCond %{SERVER_PORT} ^443$ 
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

但在安全页面上我得到循环转发。我该如何避免这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用以下两条规则:

RewriteCond %{HTTPS} off
RewriteRule ^secure https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTPS} on
RewriteRule !^secure http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]