Redirecionar loop ao forçar o HTTPS

1

Estou usando o seguinte para forçar o HTTPS no meu site principal.

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

Se você quiser ver o arquivo completo do htaccess, eu o coloquei abaixo:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    ### Blacklist via Referrers

    RewriteCond %{HTTP_REFERER} removed\.net [NC,OR]
    RewriteCond %{HTTP_REFERER} removed\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} removed\.removed\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} removed\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} removed\.net [NC,OR]
    RewriteCond %{HTTP_REFERER} removed\.sx [NC,OR]
    RewriteCond %{HTTP_REFERER} removed\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} removed\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} removed\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} removed\.com [NC,OR]
    RewriteCond %{HTTP_REFERER} removed\.org [NC]
    RewriteRule ^(.*)$ - [F,L]

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

    ### Canonicalize codeigniter URLs

    # If your default controller is something other than
    # "welcome" you should probably change this
    RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
    RewriteRule ^(.*)/index/?$ $1 [L,R=301]

    # Removes trailing slashes (prevents SEO duplicate content issues)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ $1 [L,R=301]

    # Enforce NO www
    RewriteCond %{HTTP_HOST} ^www [NC]
    RewriteRule ^(.*)$ https://removed.com/$1 [L,R=301]

    # Removes access to the system folder by users.
    # Additionally this will allow you to create a System.php controller,
    # previously this would not have been possible.
    # 'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>

    # Without mod_rewrite, route 404's to the front controller
    ErrorDocument 404 /index.php

</IfModule>

Estou lutando para descobrir como o loop de redirecionamento está acontecendo.

Atualizar

Eu não deixei claro que o meu servidor web (Apache) só é configurado com um host virtual na porta 80 que recebe tráfego HTTP e HTTPS direcionado a ele pelo balanceador de carga. O balanceador de carga lida com a conexão SSL.

    
por Abs 23.03.2015 / 13:14

2 respostas

12

Graças ao comentário HBruijn , entendi por que estava recebendo o redirecionamento loop.

O tráfego do ELB sempre será HTTP, pois ele manipula o tráfego HTTPS para o usuário, mas para o servidor é HTTP, portanto, minha regra anterior resultará em um loop.

Após algumas pesquisas, descobri que o balanceador de carga encaminha alguns cabeçalhos de usuário, como X-Forwarded-Proto . Isso pode ser usado para determinar se o cliente está usando HTTP ou HTTPS da seguinte forma:

### Force HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Espero que o texto acima ajude alguém ao tentar forçar o HTTPS por trás do balanceador de carga do Amazon Web Services.

    
por 26.03.2015 / 11:27
4

Em vez de fazer isso em um arquivo .htaccess , basta definir o SSL Redirecionar na entrada não-SSL VirtualHost da sua configuração do Apache:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName www.example.com
   Redirect permanent / https://www.example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName www.example.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>

que é muito mais eficiente.

Além disso: minha implicância, citada no manual em arquivos .htaccess :

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block in the main Apache configuration file(s), as it will have the same effect with better performance.

    
por 23.03.2015 / 13:23