htaccess rule for adding https

Asked

Viewed 1,228 times

2

I am trying to create a rule within my htaccess to add the url to HTTPS. It turns out that this should only happen for a specific domain, how to treat this in a rewritecond http_host?

example:

RewriteCond %{HTTP_HOST} palavraChave
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

would be possible?

2 answers

1

You can do it like this.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^suaurl\.com.br [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
  • Rewritecond %{HTTP_HOST} suaurl.com.br [NC] , and if you have more after com.br?

1

I know two methods:

Important: Do not force use of HTTPS if your site does not have SSL certificate.

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Or

    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80 /*porta que utiliza*/
    RewriteRule ^(.*)$ https://site.com.br/$1 [R,L]
    /*Linha 2: Condiciona que todo acesso vindo da porta 80 será afetado pela regra;*/
    /*Linha 3: Definição da regra, neste caso, sempre utilizar o https:// mesmo quando acessado por http.*/

UPDATE1

For only 1 domain:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^seudominio\.com [NC] /* aqui é como um if */
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}

or if I preferred something clearer, but does the same thing:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?seudominio.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [NC,L,R]

If it is useful, do not forget to evaluate, it helps a lot, abs

  • Thank you, then, I have SSL certificate only for 1 domain, however I have two domains on the same server and tals. So the idea of doing redir only if the host (I understand it to be www.site.com.br) is the specific domain that enabled SSL, does that make sense? If yes, how to write rewriteCod?

  • Yes, with this "Rewriteengine On Rewritecond %{HTTP_HOST} seudominio.com [NC] Rewritecond %{HTTPS} off Rewriterule (.*)$ https://%{HTTP_HOST}%{REQUEST_URI}"

Browser other questions tagged

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