Force www and https in url - HTACCESS

Asked

Viewed 2,190 times

3

Below is the code to force www and https at the url of the page using htaccess, who need 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]
  • 6

    I am voting to close this question as out of scope because it is not a question.

1 answer

9


I believe that using HTTP_HOST it is better to migrate or take advantage in multiple domains and REQUEST_URI add path and querystring

I’d 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]
  • Guilherme, good night. But is it just this code? Don’t need to pass the url? It’s the main domain and the subdomains.

  • @Thiagoferreira HTTP_HOST is a variable that contains the Host and the REQUEST_URI contains the path and querystring, so no, you don’t need it. As I said if you change the domain is easier, you don’t need to adjust anything in . htaccess

Browser other questions tagged

You are not signed in. Login or sign up in order to post.