HTTPS/HTTP redirects via .htaccess
Posted
by
Winston
on Server Fault
See other posts from Server Fault
or by Winston
Published on 2012-03-19T10:20:50Z
Indexed on
2012/03/20
11:31 UTC
Read the original article
Hit count: 286
I have a somehow complicated problem I am trying to solve. I've used the following .htaccess directive to enable some sort of Pretty URLs, and that worked fine. For example, http://myurl.com/shop would be redirected to http://myurl.com/index.php/shop, and that was well working (note that stuff such as myurl.com/css/mycss.css) does not get redirected:
RewriteEngine on
RewriteCond ${REQUEST_URI} !^(index\.php$)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^/?(.*)$ index.php/$1 [L]
But now, as I have introduced SSL to my webpage, I want the following behaviour: I basically want the above behaviour for all pages except admin.php and login.php. Requests to those two pages should be redirected to the HTTPS part, whereas all other requests should be processed as specified above.
I have come up with the following .htaccess, but it does not work. h*tps://myurl.com/shop does not get redirected to h*tp://myurl.com/index.php/shop, and h*tp://myurl.com/admin.php does not get redirected to h*tps://myurl.com/admin.php.
RewriteEngine on
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^(admin\.php$|login\.php$)
RewriteRule ^(.*)$ http://%{HTTP_HOST}/${REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^(admin\.php$|login\.php$)
RewriteRule ^(.*)$ https://myurl.com/%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_URI} !^(index\.php$)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^/?(.*)$ index.php/$1 [L]
I know it has something to do with rules overwriting each other, but I am not sure since my knowledge of Apache is quite limited. How could I fix this apparently not that difficult problem, and how could I make my .htaccess more compact and elegant?
Help is very much appreciated, thank you!
© Server Fault or respective owner