Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing SSL and WWW using .htaccess

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]   
like image 602
user2406937 Avatar asked Jul 12 '14 08:07

user2406937


People also ask

How do I redirect www to non www using htaccess?

E.g.: RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^www.example.com/$ [NC] RewriteRule ^(. *)$ http://www.example.com [R=301,L] ... @BobbyS I used the solution in this article. It redirects www and HTTP to non-www HTTPS and also handles the trailing / .


1 Answers

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] 
like image 181
Aley Avatar answered Sep 28 '22 20:09

Aley