强制 HTTPS 时重定向循环

Abs*_*Abs 1 ssl https .htaccess amazon-elb

我正在使用以下内容在我的主网站上强制使用 HTTPS。

### Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)

如果你想查看完整的 htaccess 文件,我把它放在下面:

<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>
Run Code Online (Sandbox Code Playgroud)

我正在努力弄清楚重定向循环是如何发生的。

更新

我没有明确说明我的 Web 服务器 (Apache) 仅在端口 80 上配置了一台虚拟主机,该主机将 HTTP 和 HTTPS 流量通过负载均衡器定向到它。负载平衡器处理 SSL 连接。

Abs*_*Abs 15

多亏了HBruijn 的评论,我才明白为什么我会得到重定向循环。

来自 ELB 的流量将始终是 HTTP,因为它处理到用户的 HTTPS 流量,但到服务器的流量是 HTTP,所以我之前的规则将导致循环。

经过一番研究,我发现负载均衡器会转发一些有用的标头,例如X-Forwarded-Proto. 这可用于确定客户端是使用 HTTP 还是 HTTPS,如下所示:

### Force HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Run Code Online (Sandbox Code Playgroud)

我希望以上内容可以帮助其他人尝试在 Amazon Web Services 负载均衡器后面强制使用 HTTPS。

  • 为我工作。如果相关的话,我正在使用 cloudflare。 (2认同)

HBr*_*ijn 5

无需在.htaccess文件中执行此操作,只需在 Apache 配置的非 SSL VirtualHost 条目中设置 SSL重定向

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>
Run Code Online (Sandbox Code Playgroud)

这效率更高。

另外:我的宠儿,引用自.htaccess文件的手册

如果您有权访问 httpd 主服务器配置文件,则应完全避免使用 .htaccess 文件。使用 .htaccess 文件会降低 Apache http 服务器的速度。您可以在 .htaccess 文件中包含的任何指令最好设置在主 Apache 配置文件的块中,因为它具有相同的效果和更好的性能。Directory

  • 我包含了链接,所以你可以[阅读精美手册](http://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect),你会看到`http://www.example .com/something?else=true` 将被重定向到 `https://www.example.com/something?else=true` 而不是主页。- 关于`.htaccess`,我要说的是:许多人使用它们并继续复制它们,而实际上并不了解它们的用例或影响。 (2认同)
  • 这将是包含在您原始问题中的有用信息!- 所以请允许我翻译:您在负载均衡器中进行 SSL 卸载。翻译:对您的网络服务器的所有请求都是纯 HTTP,无论客户端使用的实际协议如何。然后,您希望*在网络服务器上* 运行检查以强制执行永远不会在那里看到的 SSL 连接。 (2认同)