Force 'www' and https in url - HTACCESS [closed]

3

Below is the code to force www and https in the url of the page using htaccess, who needs it, for me it worked right. Hugs

RewriteEngine On

RewriteCond %{HTTPS} off [OR]

RewriteCond %{HTTP_HOST} !^www\.site\.com\.br$ [NC]

RewriteRule ^(.*)$ https://www.site.com.br/$1 [L,R=301]
    
asked by anonymous 15.02.2017 / 23:46

1 answer

9

I think using HTTP_HOST is better to migrate or leverage multiple domains and REQUEST_URI adds path and querystring

I would say this simplifies:

# Redireciona para o HTTPS independente do domínio
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# Adiciona www. no prefixo do domínio
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
16.02.2017 / 00:55