如何在.htaccess中切换http和https?

时间:2010-02-17 19:56:40

标签: http .htaccess https

我知道这个问题已被问过几个不同的方式,我看了/尝试了很多建议,但没有到达任何地方。

我有一个混合了http和https的网站,其中包括任何子目录的所有/客户和/购物车都是https,其余的是http。我有一个问题,实际上它去https,似乎它转到https,然后回到http。

RewriteEngine On

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule (.*) /public.php?debug=%{SERVER_NAME} [NS,QSA,L]

# Redirect to HTTPS if /cart or /customer
RewriteCond %{REQUEST_URI} ^/cart.*
RewriteCond %{REQUEST_URI} ^/customer.*
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R]

# go back to regular http if not in secure area
RewriteCond %{REQUEST_URI} !^/cart.*
RewriteCond %{REQUEST_URI} !^/customer.*
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R]

#simulate the static pages
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /public.php?controller=index&action=index [L]

#Main rewrite for application/controller/action decode logic

#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !/scripts/
RewriteCond %{REQUEST_FILENAME} !/images/
RewriteCond %{REQUEST_FILENAME} !/css/
RewriteRule ^([a-z]+)\/([a-z]+)$ /public.php?controller=$1&action=$2 [QSA,L]
RewriteRule ^([a-z]+)\/$ /public.php?controller=$1 [QSA,L]
RewriteRule ^([a-z]+)\/([a-z]+)$ /$1/$2/ [QSA,L,R]
RewriteRule ^([a-z]+)$ /$1/ [QSA,L,R]

AddHandler php5-script .php

也许有人可以直截了当地解决这个问题。

TIA

2 个答案:

答案 0 :(得分:0)

我认为你的问题是RewriteCond规则组合在一起就像一个'AND'而不是'OR',所以路径必须匹配'cart'和'customer'才能应用rewriterule(这不会合理)。试试这个..

# redirect non-https requests for /cart or /customer to https
RewriteCond %{HTTPS} off
RewriteRule ^(cart|customer) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

# redirect all other https requests to http
RewriteCond %{HTTPS} on
RewriteCond $1 !^(cart|customer)
RewriteRule ^(.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

答案 1 :(得分:0)

您可以针对类似问题查看此问题 htaccess (https to http)

即使您正常工作,您的网页也会被半加密,浏览器会在状态栏中显示红色标记。您也需要在条件中使用http referrer。