Redirect HTTP to HTTPS on . htaccess with Let’s Encrypt

Asked

Viewed 1,661 times

4

I cannot in any way test whether my address is in https when Let’s Encrypt is installed...

Some charitable soul could help me redirect to https?

I have installed in my hosting Uolhost (Platform: Linux, Apache: Version 2.4) the Let’s Encrypt... And I don’t know if there is any direct relationship (I’m still starting my life in programming and know very little of English) between the command:

Rewritecond %{HTTPS} off And the validation Let’s Encrypt... I believe it should be something related to the type of validation that is being done by Encrypt that prevents the Rewritecond command from checking the host...

Currently it is so my . htaccess

<files ~ "^.*\.([Hh][Tt][Aa])">
    order allow,deny
    deny from all
    satisfy all
</files>
Options -Indexes
Options +FollowSymLinks
ErrorDocument 400 /redirects/400.html
ErrorDocument 401 /redirects/401.html
ErrorDocument 403 /redirects/403.html
ErrorDocument 404 /redirects/404.html

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    #Tentativas RewriteCond %{HTTPS} (FALHA TUDO QUE TENTEI)
    RewriteCond %{HTTP_HOST} ^exemplo\.com\.br [NC] #verifica se falta www (MENOR IDEIA DO PQ DO NC)
    RewriteRule ^(.*)$ https://www.exemplo.com.br/$1 [R=301,L] #301 Moved Permanently
    #Atualmente consegue redirecionar de exemplo.com.br para https://www.exemplo.com.br
</IfModule>

I tried to set up:

RewriteCond %{HTTPS} off [OR] #não funcionou
RewriteCond %{HTTPS} =off [OR] #não funcionou
RewriteCond %{SERVER_PORT} 80 [OR] #não funcionou
RewriteCond %{SERVER_PORT} =80 [OR] #não funcionou
RewriteCond %{HTTP_HOST} ^exemplo\.com\.br [NC]
RewriteRule ^(.*)$ https://www.exemplo.com.br/$1 [R=301,L]

always returns the error:

"This page is not working

Excessive redirection by www.exemplo.com.br

Try to clear the cookies.

ERR_TOO_MANY_REDIRECTS"

(BS.: ) I put my theory to the test and uninstalled Let’s Encrypt...

RewriteCond %{HTTPS} =off [OR] #funcionou
RewriteCond %{SERVER_PORT} =80 [OR] #funcionou

Redirecting worked but obviously it doesn’t solve my problem because I don’t have a certificate without Let’s Encrypt installed.

The problem is in the process of validating Let’s Encrypt... I need to find another way to redirect, maybe without using Rewriterule.

Another discussion of the same topic

https://cursos.alura.com.br/forum/topico-redirecionar-http-https-com-htaccess-39055

  • This ERR_TOO_MANY_REDIRECTS is suspicious, do the following, try in this way https://answall.com/a/184459/3635 and let me know what the result is, note, open the browser test window anonymously.

  • Good evening @Guilhermenascimento. Incredibly, your answer was one of the first I looked at and tried. It doesn’t work... when I inspect what happens is that it redirects infinitely, in a kind of "loop" 301 to https://www.exemplo.com.br... As I said, "Rewritecond" cannot check if it is on "https" when I install Let’s Encrypt ps.: sorry for the delay, I was having dinner

  • There must be something you have added to more or some other . htaccess in another folder.

  • I just checked.. There is no other .htaccess... This behavior does not exist when I give up Let’s Encrypt. Normal redirect

  • You installed Let’s Encrypt on your server or localhost (I don’t even know if it’s possible)?

  • I installed in my hosting uolhost.. the process eh automated.. just click install and ready, the certificate comes to work (valid for 3 months)

  • It may be something uolhost is programmed to redirect, I will try to install the certificate on a server to see if it occurs, when I can warn you

  • Thank you very much! Just emphasizing that the problem occurs specifically when I test "Rewritecond %{SERVER_PORT} =80" (or something like that)... removing this line from htaccess, normally redirects the typed domain without www to www

  • I don’t think there’s much point checking the door, it would only be necessary to get the protocol, it must be something else the problem.

  • so when I test the protocol also enters the loop, I tested both via port and protocol

Show 5 more comments

1 answer

2


I also use Let’s Encrypt and had a similar problem, but I solved it on Virtualhosts from Apache.

As in your case, you only have access to .htaccess, tested some methods on a server of mine, and brought what worked best.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Verifica www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]

# Verifica requisições POST
RewriteCond %{REQUEST_METHOD} !^POST$

# Se estiver recebendo uma requisição http de um proxy...
RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]

#...ou só uma requisição diretamente do cliente
RewriteCond %{HTTP:X-Forwarded-Proto} =""
RewriteCond %{HTTPS} !=on

# Redireciona para a versão HTTPS
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

</IfModule>

I believe that this solution also works for your case.

  • Wonderful!!!!! It did work! Thank you very much! (Y)

Browser other questions tagged

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