Harold Dunn
Harold Dunn

Reputation: 1459

Redirect entire site to https

I want to redirect my entire site, including all subdomains to https.

Will this work?

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Upvotes: 2

Views: 1224

Answers (2)

anubhava
anubhava

Reputation: 784958

While your code may work alright but I would recommend using %{HTTP_HOST} instead of %{SERVER_NAME}. Please understand the difference between 2 variables. %{SERVER_NAME} is the name of the server as configured in your Apache config but later one is the domain name received in real time in HTTP request. So instead of your code use this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} Off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

In order to exclude above rule for a particular sub-domain use something like this code:

RewriteCond %{HTTP_HOST} !^sub\.domain\.com$ [NC]
RewriteCond %{HTTPS} Off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Upvotes: 1

Jon Lin
Jon Lin

Reputation: 143866

Yes, that will work.

The important thing is to make sure that rule is before any routing rules you may have and that no other rules redirect to http://.

Upvotes: 0

Related Questions