Redirect HTTP to HTTPS in .htaccess with Let's Encrypt

4

I can not in any way test if my address is on https when let's encrypt is installed ...

Could some charitable soul help me redirect to https?

I have installed on my hosting UOLHost (Platform: Linux, Apache: Version 2.4) let's encrypt ... And I do not know if there is any direct relationship (I'm still starting my programming life and I know very little 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 being done by encrypt that prevents the RewriteCond command from checking the host ...

Currently this is 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 configure:

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 error:

"This page is not working

Excess redirect through www.example.com

Try to clear cookies.

ERR_TOO_MANY_REDIRECTS "

(obs. :) I tested my theory and uninstalled let's encrypt ...

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

The redirection worked but obviously does not solve my problem because I do not have a certificate without the let's encrypt installed.

The problem is in the validation process of let's encrypt ... I need to find another way to redirect, perhaps without using RewriteRule.

Another discussion of the same topic

link

    
asked by anonymous 30.05.2017 / 01:08

1 answer

1

I also used Let's Encrypt and I had a similar problem, but I solved it in Apache VirtualHosts.

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 this solution works for you as well.

    
04.06.2017 / 16:32