【问题标题】:How do I switch between http and https in .htaccess?如何在 .htaccess 中在 http 和 https 之间切换?
【发布时间】:2010-02-19 18:15:21
【问题描述】:

我知道这个问题已经被问过几种不同的方式,我已经查看/尝试了许多建议,但没有得到任何结果。

我有一个混合了 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

【问题讨论】:

    标签: http .htaccess https


    【解决方案1】:

    我认为您的问题是 RewriteCond 规则像“AND”而不是“OR”一样组合在一起,因此路径必须匹配“cart”和“customer”才能应用重写规则(这不会说得通)。试试这个..

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

    【讨论】:

      【解决方案2】:

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

      即使你让它工作,你的页面也会被半加密,并且浏览器会在状态栏显示一个红色标记。您还需要在条件中使用 http referrer。

      【讨论】:

        猜你喜欢
        • 2010-11-09
        • 2011-10-20
        • 2014-01-11
        • 2015-02-18
        • 1970-01-01
        • 2012-08-17
        • 2012-05-14
        • 1970-01-01
        • 2015-04-02
        相关资源
        最近更新 更多