【问题标题】:Force HTTPS on certain URLs and force HTTP for all others对某些 URL 强制使用 HTTPS,对所有其他 URL 强制使用 HTTP
【发布时间】:2011-03-19 10:23:55
【问题描述】:

我有一个客户端项目,我需要为某个文件夹强制使用 HTTPS,并为所有其他文件夹强制使用 HTTP。我可以成功地为我想要的文件夹强制实施 HTTPS,但所有返回站点其余部分的链接最终都通过 HTTPS。我想要一个规则,强制对安全文件夹中任何“不”的请求强制返回 HTTP。到目前为止,这是我所拥有的:

RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]

RewriteCond %{HTTPS} !=on
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1

'my' 是我需要为其强制使用 HTTPS 的文件夹的名称。

有什么想法吗?

更新:我也试过了:

RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]

# Force HTTPS for /my
RewriteCond %{HTTPS} !=on
RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

# Force HTTP for anything which isn't /my
RewriteCond %{HTTPS} =on
RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

# Remove index.php from URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1

但现在它们不再通过 HTTPS 强制对 /my 的请求解析为 http://www.example.com/index.php/my

:?

【问题讨论】:

    标签: apache http mod-rewrite https


    【解决方案1】:

    只需反转条件:

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

    【讨论】:

    • 您好,感谢您的快速回复。我试过了,但我不确定将它放在我现有规则流中的哪个位置,有什么指示吗?如果我将它放在现有的“强制 HTTPS”规则之上,它会阻止“强制 HTTPS”工作,并且还会阻止重写 index.php 的最终规则执行。 :?
    • @Nathan Pitman:不,因为他们有不同的匹配条件,他们不应该互动。
    • 好的,我尝试了您的建议(请参阅我原始帖子的更新),但这不起作用。我可以看到它“应该”,但我认为我的规则可能顺序错误,或者某些规则导致重写在其他条件和规则可以被评估之前终止......:/
    • @Nathan Pitman:或者你没有提到你在httpd.conf(或类似的)中定义规则,每个人都认为它在.htaccess,在这种情况下你需要确保所有测试模式都匹配^/,例如^/my(我可能是错的,这只是一个想法)
    • 嗨,蒂姆,是的,这是在 .htaccess 中定义的
    【解决方案2】:

    这是适用于旧客户网站的内容,可以根据您的目的进行调整:

    #If https off and in the cart dir
    RewriteCond %{HTTPS} =off [NC]
    RewriteCond %{REQUEST_URI} ^/cart/(.*) [NC]
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/cart/%1 [R=301,L]
    
    #If https on and not in cart dir    
    RewriteCond %{HTTPS} =on
    RewriteCond %{REQUEST_URI} !^/cart [NC]
    #Above line actually used to read RewriteCond %{REQUEST_URI} !^/cart|media|images|thumbs|css|js [NC]
    #to allow js/css/images to be served so there were no mixed ssl messages popping up to visitors
    RewriteCond %{REQUEST_FILENAME} !index\.php$ [NC]
    RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
    

    用我的可能替换购物车

    【讨论】:

      【解决方案3】:

      试试下面的,应该对你有用:

      RewriteEngine On
      
      RewriteCond %{HTTPS} off
      RewriteCond %{REQUEST_URI} ^/my
      RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
      
      RewriteCond %{HTTPS} on
      RewriteCond %{REQUEST_URI} !^/my
      RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
      

      【讨论】:

        【解决方案4】:

        啊,当然。问题在于您的重写规则集将在初始重定向后转换为index.php 后重新处理。使用您当前拥有的内容,您需要额外调整重定向以确保在重写为 /index.php/my 后它们不会被应用。

        应该执行以下操作:

        RewriteEngine On
        RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
        
        # Force HTTPS for /my
        RewriteCond %{HTTPS} !=on
        RewriteCond %{THE_REQUEST} ^[A-Z]+\s/my [NC]
        RewriteRule ^(my) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
        
        # Force HTTP for anything which isn't /my
        RewriteCond %{HTTPS} =on
        RewriteCond %{THE_REQUEST} !^[A-Z]+\s/my [NC]
        RewriteRule !^my http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
        
        # Remove index.php from URLs
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ /index.php/$1
        

        【讨论】:

        • 谢谢,节省了我很多时间! FWIW,在 Wordpress 中,基本的 Wordpress htaccess IfModule 已经从 URL 中删除了 index.php。
        猜你喜欢
        • 2014-01-22
        • 2011-02-15
        • 1970-01-01
        • 1970-01-01
        • 2015-06-22
        • 2012-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多