简体   繁体   English

Apache重定向将https设置为http并匹配和不匹配

[英]Apache Redirect setting https to http and matching & not matching

I am confusing about redirect setting by apache. 我对apache的重定向设置感到困惑。 It looks simple but I can't figure out… 看起来很简单,但我不知道…

Under http access 在http访问下

 if carts/ and events/* need to redirect to https

I am adding this setting in sites-enabled/default-http.conf 我将此设置添加到启用了site / default-http.conf的站点中

<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/carts/ [OR]
RewriteCond %{REQUEST_URI} ^/events/*
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</ifModule>

Under https access 在https访问下

 if not carts/ and events/* need to redirect to http

I am adding this setting in sites-enabled/default-https.conf 我在启用了site / default-https.conf的站点中添加了此设置

<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !(^/carts/) [OR]
RewriteCond %{REQUEST_URI} !(^/events/*)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</ifModule>

The problem is this 问题是

http is working well however https does not work. http工作正常,但是https无法正常工作。
https://test.com/test should redirect to http://test.com/test https://test.com/test应该重定向到http://test.com/test

First of all to avoid restart of Apache enable .htaccess (if not enabled already) then use these rules in site root .htaccess: 首先,避免重新启动Apache启用.htaccess(如果尚未启用),然后在站点根.htaccess中使用以下规则:

RewriteEngine On

RewriteCond %{THE_REQUEST} /(carts|events)/ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]

RewriteCond %{THE_REQUEST} !/(carts|events)/ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]

Test this in a new browser to avoid old browser cache. 在新的浏览器中进行测试,以避免旧的浏览器缓存。 And preferable test in chrome dev tool to see what redirects you're getting. 并在chrome dev工具中进行了更好的测试,以查看您获得的重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM