mage_dev
mage_dev

Reputation: 11

.htaccess redirect to "https://www"

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.

Upvotes: 1

Views: 502

Answers (1)

Stefan
Stefan

Reputation: 5530

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

Upvotes: 1

Related Questions