Force ‘www’ and https in url – HTACCESS [closed]

Posted on

Question :

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]

    

Answer :

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]

    

Leave a Reply

Your email address will not be published. Required fields are marked *