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

如何在.htaccess中切换http和https?,http,.htaccess,https,Http,.htaccess,Https,我知道这个问题已经被问了好几种不同的方式,我已经看了/尝试了许多建议,但没有取得任何进展 我有一个混合了http和https的站点,其中所有/customer和/cart(包括任何子目录)都是https,其余都是http。我有一个问题,它实际上去了https,似乎它去了https,然后又回到了http RewriteEngine On #RewriteCond %{REQUEST_FILENAME} !-f #RewriteRule (.*) /public.php?debug=%{SERVE

我知道这个问题已经被问了好几种不同的方式,我已经看了/尝试了许多建议,但没有取得任何进展

我有一个混合了http和https的站点,其中所有/customer和/cart(包括任何子目录)都是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

我认为您的问题在于,RewriteCond规则像“AND”而不是“OR”那样组合在一起,因此路径必须匹配“cart”和“customer”才能应用RewriteCond规则(这没有意义)。试试这个

# 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]

对于类似的问题,你可以看看这个问题

即使你让它工作,你的页面将被半加密,浏览器将在状态栏上显示一个红色标记。在这种情况下,您也需要使用http Referer