Apache – mod_rewrite – redirect一些pathHTTPS到HTTP

我的公司希望我们的网站强制大部分网站的http协议,除了“用户”,“商店”和“购物车”三个部分。 我使用http://htaccess.madewithlove.be/上的在线.htaccesstesting程序来构build我的文件,在那里似乎是正确的。 但是,当我实际执行它,我有一个问题。 如果我去https的任何常规网页的作品,我redirect到http版本就好了。 但是,如果我去'/ shop','/ cart',或'/ user',不pipe是用http还是https,我只是用httpredirect到主页。 这是.htaccess文件:

RewriteEngion on # Force HTTPS for /shop RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} /shop RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] # Force HTTPS for /cart RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} /cart RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] # Force HTTPS for /user RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} /user RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] # Force HTTP for all others RewriteCond %{HTTPS} on RewriteCond %{REQUEST_URI} !/cart [NC] RewriteCond %{REQUEST_URI} !/shop [NC] RewriteCond %{REQUEST_URI} !/user [NC] RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L] # Pass all requests not referring directly to files in the filesystem to # index.php. Clean URLs are handled in drupal_environment_initialize(). RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^ index.php [L] 

您需要在您的条件下使用THE_REQUEST变量。 THE_REQUEST变量表示Apache从浏览器接收到的原始请求,并且在执行某些重写规则后不会被覆盖。 这个变量的值是GET /index.php?id=123 HTTP/1.1

  # Force HTTPS for cart, shop, user RewriteCond %{HTTPS} off RewriteCond %{THE_REQUEST} \s/(cart|shop|user) [NC] RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L] # Force HTTP for all others RewriteCond %{HTTPS} on RewriteCond %{THE_REQUEST} !\s/(cart|shop|user) [NC] RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L] # Pass all requests not referring directly to files in the filesystem to # index.php. Clean URLs are handled in drupal_environment_initialize(). RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !=/favicon.ico RewriteRule ^ index.php [L]