Yas
Yas

Reputation: 465

htaccess redirect to HTTPS

I've been trying to figure out a way to redirect only one domain to HTTPS but haven't found a good solution. I was able to redirect all requests to https by using the HTTPS!=on condition but I host multiple domains and only one has SSL.

This gave me some success.

RewriteCond %{HTTP_HOST} ^(127\.0\.0\.1|localhost)$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

But it doesn't seem to redirect URLs like www.mydomain.com/order/ and mydomain.com/order/

Basically it works only for the main page currently at www.mydomain.com or mydomain.com.

What am I missing?

Upvotes: 2

Views: 237

Answers (1)

elixenide
elixenide

Reputation: 44851

Your problem is this:

RewriteCond %{HTTP_HOST} ^(127\.0\.0\.1|localhost)$

That says, "Only use the next RewriteRule if the host is 127.0.0.1 or localhost, exactly." If you host mydomain.com on that same server, it won't match. You need to add the name of the domain you want to redirect. Example:

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Upvotes: 1

Related Questions