Apache强制HTTP的某些URL和所有其他URL通过https

时间:2013-09-10 17:42:13

标签: regex apache mod-rewrite url-rewriting https

首先我要说我找到了许多修改情况的答案,但不是为此。

Apache强制HTTP的某些URL,所有其他URL通过https。 想获得关于以下的专业知识。感谢您的所有时间并感谢。

我们在生产环境中有tomcat前端tomcat,我们希望为所有传入启用https,除了几页,并希望帮助编写可靠的apache重写规则来执行此操作。 那是

  1. 除了某些页面(/ abc,/ def,/ ghi)之外,所有传入连接都通过https进行强制http。
  2. 任何帮助。我在谷歌help的帮助下想出了这个,但它不适用于http。所有流量都为https

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

1 个答案:

答案 0 :(得分:2)

你的条件中有太多的斜杠来检查%{REQUEST_URI}。删除括号内的那些:

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/(abc|def|ghi)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/(abc|def|ghi)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]