htaccess将所有页面重定向到https,除非设置了cookie

时间:2013-10-29 20:37:58

标签: .htaccess redirect cookies https

默认情况下,我需要网站将所有内容重定向到HTTPS,但仅限于将Cookie设置为“admin”时。当cookie设置为“admin”时,我需要将整个站点设为http,而不仅仅是某些页面。到目前为止,我已经在这里和那里阅读了很多条目,但实际上并没有。以下是我对htaccess的看法:

RewriteCond %{HTTP_COOKIE} (admin)
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://www.domain.org/$1 [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.org/$1 [R,L]

也尝试了

RewriteCond %{HTTP_COOKIE} cookie-name=admin
RewriteCond %{HTTPS} !=off
RewriteRule ^(.*)$ http://www.domain.org/$1 [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_COOKIE} !cookie-name=admin
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.domain.org/$1 [R,L]

即使已经设置了cookie,它仍然会将我重定向到https。有没有办法解决这个问题?谢谢!

1 个答案:

答案 0 :(得分:1)

尝试:

RewriteCond %{HTTP_COOKIE} !admin
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.org/$1 [R,L]

RewriteCond %{HTTP_COOKIE} admin
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ http://www.domain.org/$1 [R,L]