简体   繁体   English

如何有选择地重定向到https / http

[英]How to selectively redirect to https/http

I need to incorporate some pages in site to https. 我需要将站点中的某些页面合并到https中。 Now what I want following two cases . 现在我要下面两种情况。

  1. If page REQUEST_URI are user, user/login, user/register, cart, checkout AND original protocol is http. 如果页面REQUEST_URI是用户,用户/登录名,用户/注册,购物车,结帐且原始协议为http。 Redirection to https should happen. 重定向到https应该发生。
  2. If page REQUEST_URI are other than user, user/login, user/register, cart, checkout AND original protocol is https. 如果页面REQUEST_URI不是用户,用户/登录名,用户/注册,购物车,结帐和原始协议为https,则其他页面为https。 Redirection to http should happen. 重定向到http应该会发生。

So What I implemented the following Rules . 所以我实现了以下规则。

# Block 1 - Forcing HTTPS
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{SERVER_PORT} 80

# Forcing HTTPS
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_URI} ^/cart  [OR]
RewriteCond %{REQUEST_URI} ^/checkout  [OR]
RewriteCond %{REQUEST_URI} ^/user/login [OR]
RewriteCond %{REQUEST_URI} ^/user [OR]
RewriteCond %{REQUEST_URI} ^/user/register
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]


# Block 2 - Forcing HTTP
RewriteCond %{HTTPS} !=off [OR]
RewriteCond %{SERVER_PORT} 443

RewriteCond %{SERVER_PORT} !^80$
RewriteCond %{REQUEST_URI} !^/cart 
RewriteCond %{REQUEST_URI} !^/checkout 
RewriteCond %{REQUEST_URI} !^/user/login
RewriteCond %{REQUEST_URI} !^/user
RewriteCond %{REQUEST_URI} !^/user/register
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,QSA,L]


# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

Here I facing problem, If one of two block is enabled functionality run smoothly according to rules. 在这里我面临的问题是,如果启用了两个块之一,则功能会根据规则平稳运行。 But if both were enabled, recursive redirection start to happen. 但是,如果同时启用了两者,则递归重定向将开始发生。 I need both functionality. 我需要两个功能。 If it is possible through .htaccess rules ? 是否可以通过.htaccess规则?

UPDATE : The similar question asked on SO as how in htaccess can i redirect the user to https from http and back again . 更新:类似的问题在SO上问到, 如何在htaccess中将用户从http重定向到https并再次返回 But it only have one answer suggesting to move on applciation side. 但是它只有一个建议建议朝着应用程序的方向发展。

UPDATE 2 : Added Zend Framework Application default .htaccess rule 更新2:添加了Zend Framework Application默认.htaccess规则

You have made it pretty complex. 您已经使其变得非常复杂。 Simplify your rules like this: 像这样简化您的规则:

RewriteEngine On

# Block 1 - Forcing HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(cart|checkout|user|user/login|user/register)/?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,NE,L]

# Block 2 - Forcing HTTP
RewriteCond %{HTTPS} on [OR]
RewriteCond %{SERVER_PORT} 443
RewriteRule !^(cart|checkout|user|user/login|user/register)/?$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,NE,L]

Make sure to test in a new browser to avoid 301 caching issues. 确保在新的浏览器中进行测试,以避免301缓存问题。

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

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