我在here之前提出了疑问。我认为它有效,但实际上还没有。
我有负载平衡器面向Internet,后面是Ec2实例。 我的负载均衡器打开到端口80和端口443。
它们被安排为80-> 80和443 - > 80。
现在我需要将http访问重定向到https。
我做的是 我制作了一个.htaccess文件并放在/ var / www / html /中。那是我网站的index.php所在的文件夹。
在.htaccess文件中,我尝试了一些我在互联网上找到的版本。他们都没有工作。
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
</VirtualHost>
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP_HOST} www.(.+) [OR,NC] # Added
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC]
RewriteRule ^/?(.*) https://domainname.com%{REQUEST_URI} [L,R=301]
</VirtualHost>
为什么它在我的设置中不起作用?
编辑:
<If "req('X-Forwarded-Proto') != 'https'>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
</If>
答案 0 :(得分:0)
我使用apache为HTTPS做的是通过container commands上传自定义redirect.conf
文件。所以redirect.conf
看起来像这样:
<If "req('X-Forwarded-Proto') != 'https'>
RewriteEngine On
RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</If>
然后你将使用容器命令将此文件放入apache配置目录/etc/httpd/conf.d/
,如下所示:
container_commands:
01_update_rewrite_rules:
command: "cp rewrite.conf /etc/httpd/conf.d/"
答案 1 :(得分:0)
我已经解决了很长一段时间的问题,现在可以使它成功运作。我想分享我的工作如下。
(1)Create .htaccess file inside the folder same as root file index.php exists
(2).htaccess file has
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule . https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
(3)Then enable mod_rewrite, try the command line command:
sudo a2enmod rewrite
(4)To make sure mod_rewrite is enabled, update /etc/apache2/apache2.conf file as
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
initially is
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Then you can setup http to https successfully
You need to restart server as sudo service apache2 restart after update of /etc/apache2/apache2.conf
您的负载均衡器及其安全组需要打开http访问。