简体   繁体   English

apache-mod_rewrite-将一些路径重定向到https到http

[英]apache - mod_rewrite - redirect some paths to https others to http

My company wants our site to force an http protocol for most of the site except for three sections 'user', 'shop', and 'cart'. 我的公司希望我们的网站对大多数网站强制使用http协议,但“用户”,“商店”和“购物车”这三个部分除外。 I used the online .htaccess tester at http://htaccess.madewithlove.be/ to build my file first, and it appeared to be correct there. 我首先使用http://htaccess.madewithlove.be/上的在线.htaccess测试仪来构建我的文件,在那看来是正确的。 However, when I actually implemented it I had a problem. 但是,当我实际实现它时,我遇到了一个问题。 If I go to https for any of the regular pages it works and I'm redirected to the http version just fine. 如果我在任何常规页面上都访问https,那么它将正常工作,并且将我重定向到http版本就可以了。 However if I go to '/shop', '/cart', or '/user' no matter if it's with http or https i just get redirected to the home page with http. 但是,如果我转到“ / shop”,“ / cart”或“ / user”,无论是使用http还是https,我都将使用http重定向到主页。 Here's the .htaccess file: 这是.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]

You need to use THE_REQUEST variable in your conditions. 您需要在条件中使用THE_REQUEST变量。 THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. THE_REQUEST变量表示Apache从您的浏览器收到的原始请求,在执行某些重写规则后不会被覆盖。 Example value of this variable is GET /index.php?id=123 HTTP/1.1 此变量的示例值为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]

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

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