I'm looking to use an .htaccess file to use mod_rewrite to switch the protocol from http:// to https:// when someone hits my website.

For instance, once someone goes to: http://www.mywebsite.com/

I'd like the browser to switch to: https://www.mywebsite.com/

The same goes for the http://mywebsite.com/ -> https://mywebsite.com

This is the following code I've been using and I've experienced some odd things so if anyone could provide me with information if this is the right way to do it, or if you have a better way, please provide it. Thanks in advance.

RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://www.ebaillv.com/$1 [R=301,L]

1 Answer 1


You could try checking for HTTPS instead of whether the port is 443. Also, the rule below matches everything and redirects it to exactly the same as it was but with the https. You could probably throw the [R=301, L] at the end of it if you wanted.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
  • Sorry for the delay. This looks like a great alternative. Thanks Bob!
    – Mike
    Jul 7, 2011 at 2:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.