35

I would like to know if this code in .htaccess for forcing SSL and WWW in URL is correct, because with another codes I usually get redirect loop, e.g. RewriteCond %{HTTPS} !=on and now it works like a charm (suspiciously). Also, is possible to write it better/simplier?

# Force to SSL
RewriteCond %{HTTP:HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Force to WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]  
2

6 Answers 6

66

That's a bit simpler.

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
Sign up to request clarification or add additional context in comments.

6 Comments

could you please explain the conditions as well? I am bit novice here.
Line 2 checks if HTTPS is off then we have an OR condition with line 3. Line 3 checks if hostname is different from www.domain.com. If one of them gives true, last line takes action and redirects "R=301" with HTTP Code 301 to domain.com
By this all the subdomains gets redirected as well. for eg. api.domain.com changes to www.domain.com.. is there any way to add www only to root domain.com and not any subdomain.
this will not redirect https://example.com to https://www.example.com
dont think that works for subfolders if you have another .htaccess on them
|
37

Use this:

RewriteEngine on

# Force www: from http://stackoverflow.com/a/4958847/1078583
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Force SSL: From http://stackoverflow.com/q/24322035/
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]

2 Comments

Is there a reason for making the SSL redirect temporary instead of permanent?
@Jabari Looks like I may have pasted these two pieces of code (see the URLs). But in general, 302s are recommended while testing, then when you're sure the redirect works, move to 301.
14

I think this is a great snippet. Not necessary to type a domain. This is forcing WWW and HTTPS in any case. This is dealing with subfolders as well.

RewriteEngine On

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

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

2 Comments

I think I prefer this one.
Works perfectly and reliably.
3

Sorry for bumping this topic but I just wanted to add a simple solution for search engine visitors.

RewriteEngine on
# Force WWW & SSL
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.stackoverflow.com/$1 [L,R=301] 

Comments

1

9 Force www: is perfect thank you.

My server is Heart Internet and the force SSL for Heart is:

# All calls go to SSL
RewriteEngine On
RewriteCond %{ENV:HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Comments

0

I had the same problem and I used this to solve it. worked best.

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L,NE]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.