Reputation: 2606
I need to incorporate some pages in site to https. Now what I want following two cases .
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 ?
UPDATE : The similar question asked on SO as how in htaccess can i redirect the user to https from http and back again . But it only have one answer suggesting to move on applciation side.
UPDATE 2 : Added Zend Framework Application default .htaccess rule
Upvotes: 1
Views: 1105
Reputation: 785098
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.
Upvotes: 1