【问题标题】:Apache Redirect setting https to http and matching & not matchingApache Redirect 将 https 设置为 http 并匹配和不匹配
【发布时间】:2015-05-19 14:14:29
【问题描述】:

我对 apache 的重定向设置感到困惑。看起来很简单,但我想不通……

http访问下

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

我在 sites-enabled/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>

https访问下

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

我在 sites-enabled/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>

问题是这样的

http 运行良好,但 https 不起作用。
https://test.com/test 应重定向到 http://test.com/test

【问题讨论】:

  • 感谢您的回复。我添加了 *.conf
  • 我添加了一个问题 :-)

标签: regex apache mod-rewrite redirect https


【解决方案1】:

首先要避免重启 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]

在新浏览器中进行测试以避免旧浏览器缓存。最好在 chrome 开发工具中进行测试,看看你得到了什么重定向。

【讨论】:

    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    相关资源
    最近更新 更多