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。
无需在.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)
这效率更高。
如果您有权访问 httpd 主服务器配置文件,则应完全避免使用 .htaccess 文件。使用 .htaccess 文件会降低 Apache http 服务器的速度。您可以在 .htaccess 文件中包含的任何指令最好设置在主 Apache 配置文件的块中,因为它具有相同的效果和更好的性能。
Directory
归档时间: |
|
查看次数: |
20281 次 |
最近记录: |