Using .htaccess To Redirect HTTPS To HTTP

Thursday, April 9, 2009 - 10:12

To redirect from HTTPS to HTTP on the home page only using the following rule.

1
2
RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://%{SERVER_NAME}/ [R=301,L]

The variable %{HTTPS} will be either "on" or "off" and will be enabled even if SSL is not installed on your site. The rule above sees that HTTPS is on and redirects the home page to the HTTP version. You can even chain lots of rules together like this.

1
2
3
RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://%{SERVER_NAME}/ [R=301]
RewriteRule ^inner/directory/?$ http://%{SERVER_NAME}/inner/directory/ [R=301,L]

Note that you should end your last rule with L so that no other rules on the page are run. Also, you need to make absolutely sure that you are not redirecting any pages that are integral to the security of your shopping cart as this will turn off HTTPS for those pages.

You can also do the same thing using the ${SERVER_PORT} variable.

1
2
RewriteCond %{SERVER_PORT} 443
RewriteRule ^/?$ http://%{SERVER_NAME}/ [R=301,L]

The port for HTTPS is 443 so if the port being communicated through is 443 we need to redirect.

Category: 
philipnorton42's picture

Philip Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK.
Google+ | Twitter

Comments

hi there,

I am trying to redirect https to http for the website coupon aid
I changed htaccess but it doesn't seem to work. Could you take a look and see what the problem is?

it's not redirecting :(

philipnorton42's picture
Submitted by philipnorton42 on Mon, 05/16/2011 - 10:04

If it isn't redirecting then there are a couple of things you can try. The rules above require mod_rewrite being available on your Apache server, if it isn't on then the rules throw and error. There may be an issue with the expression you are using to redirect if you are trying to do it with an inner directory. Either way, any errors should be logged by your Apache server in the error.log so if nothing works that should point you in the right direction.

What if you use Nginx instead of Apache?

I know this is an old thread, but I've just discovered that replacing http with https on my site redirects to a totally unrelated site (shared server). I successfully use htaccess to redirect from mysite.com to www.mysite.com but the above code isn't working.

please I need one only htaccess that:
for website1 redirect from http to https
and for others websites redirect from https to http
and please for all websites redirect "without www" to www
can you help me? Thanks a lot

Here only i learned back-end process of htaccess file.

Thank You Genius :)

Add new comment