1

I'm looking for an .htaccess configuration that will redirect all requests to my site to https://www.

So whether the user visits:

www.example.com
http://example.com
http://www.example.com
https://example.com

They will always end up at:

https://www.example.com

Here is what I am currently using in my .htaccess file, however it doesn't redirect if a user visits:

www.example.com

Here is my current file contents:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

I've tried various solutions available on Stack Overflow, but none of them seem to work as intended, often leading to redirect loops. Hopefully someone will have a better understanding of this than I do.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
mage_dev
  • 11
  • 2
  • Take a look here: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond – Laurence Jan 09 '13 at 14:50
  • Thank you, I'm reading through this now, but I feel like I'm getting lost... There is a lot of information there to sort through. – mage_dev Jan 09 '13 at 14:57
  • duplicate of http://stackoverflow.com/questions/13977851/htaccess-redirect-to-https-www?rq=1 – jvilhena Jul 29 '13 at 11:12

1 Answers1

1

Your regular expression ^example\.com doesn't match www.example.com. You probably want something like (^|\.)example\.com or (^|\.)example\.com($|:).

Edit:

To prevent the redirect loop add

RewriteCond %{HTTPS} off

Before the RewriteRule.

Also see http://wiki.apache.org/httpd/RewriteHTTPToHTTPS (they use !=on instead of off) and http://wiki.apache.org/httpd/RedirectSSL

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Stefan
  • 5,304
  • 2
  • 25
  • 44