We've got a shopping site which we're hosting on a shared host (Mediatemple Gridserver). Some parts of the site need to use HTTPS (checkout etc) but the rest should be using HTTP.
Does anyone know how we can always force the correct use of HTTP/HTTPS for particular URLs? We've had it working in various states but we can't get a request for a page that should be on HTTP but is requested with HTTPS to switch back correctly.
I've had a look around SO but couldn't find a suitable answer to this.
This should work in pretty much every scenario and should work in your actual vhost or .htaccess:
(do not forget the slash before %{REQUEST_URI} as this may allow passing a portnumber, which is dangerous)
As detailed in this answer, fix your application to use
https://
links when needed. Don't rely on automatic redirections, this could lead you to a false sense of security if you haven't made your links/forms served overhttps://
go tohttps://
URLs too. Usingmod_rewrite
automatically makes it harder to detect such mistakes (which can also be vulnerabilities).I had some problem being behind a loadballancer. This how i fixed it.
For me worked this (I used it for wordpress site and redirecting to HTTPS). You have to add the condition and rule lines just behind RewriteEngine and RewriteBase lines:
Have a look to condition
RewriteCond %{HTTP:X-Forwarded-Proto} !https
- only this worked for my server hosting. (I triedRewriteCond %{SERVER_PORT} !^443$
orRewriteCond %{HTTPS} off
as well, but without success.I use something similar to this for my admin folder in wordpress:
The
RewriteCond %{HTTPS} on
portion may not work for all web servers. My webhost requiresRewriteCond %{HTTP:X-Forwarded-SSL} on
, for instance.If you want to force the reverse, try:
If you want some alternate ways to do it, check out askapache.
I think it should be:
See the mod_rewrite documentation.