Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 使用AWS'从Http重定向到Https;易北河_.htaccess_Amazon Web Services_Amazon Ec2_Url Redirection_Amazon Elb - Fatal编程技术网

.htaccess 使用AWS'从Http重定向到Https;易北河

.htaccess 使用AWS'从Http重定向到Https;易北河,.htaccess,amazon-web-services,amazon-ec2,url-redirection,amazon-elb,.htaccess,Amazon Web Services,Amazon Ec2,Url Redirection,Amazon Elb,我以前提出过问题。我认为这是可行的,但实际上还没有 我有一个面向Internet的负载均衡器,后面是Ec2实例。 我的负载平衡器打开到端口80和端口443 它们排列为80->80和443->80 现在我需要将http访问重定向到https 我所做的是 我创建了一个.htaccess文件,位于/var/www/html/中。这就是我的网站index.php所在的文件夹 在.htaccess文件中,我尝试了一些我在互联网上找到的版本。他们都没有工作 <VirtualHost *:80>

我以前提出过问题。我认为这是可行的,但实际上还没有

我有一个面向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>

重新启动发动机
RewriteCond%{HTTP:X-Forwarded-Proto}=HTTP
RewriteCond%{HTTP\u USER\u AGENT}^ELB健康检查器
重写规则。https://%{HTTP:Host}%{REQUEST_URI}[L,R=permanent]
重新启动发动机
RewriteCond%{HTTP_HOST}www.(.+)[OR,NC]#添加
重写cond%{HTTP:X-Forwarded-Proto}!https[NC]
重写规则^/?(*))https://domainname.com%{REQUEST_URI}[L,R=301]
为什么在我的设置中不起作用

编辑:


我用apache为HTTPS做的是通过上传一个自定义的
redirect.conf
文件。因此,
redirect.conf
看起来像这样:

<If "req('X-Forwarded-Proto') != 'https'>
    RewriteEngine On
    RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</If>

我已经解决这个问题很长时间了,现在可以使它成功地工作。我想分享我所做的如下

(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 
(1)在与根文件index.php相同的文件夹中创建.htaccess文件
(2) .htaccess文件已被删除
重新启动发动机
RewriteCond%{HTTP:X-Forwarded-Proto}=HTTP
重写规则。https://%{HTTP:Host}%{REQUEST_URI}[L,R=permanent]
(3) 然后启用mod_rewrite,尝试命令行命令:
sudoa2enmod重写
(4) 要确保启用mod_rewrite,请将/etc/apache2/apache2.conf文件更新为
选项索引跟随符号链接
允许超越所有
要求所有授权
最初是
选项索引跟随符号链接
不允许超限
要求所有授权
然后您可以成功地将http设置为https
更新/etc/apache2/apache2.conf后,您需要以sudo服务apache2重启的方式重新启动服务器

您的负载平衡器及其安全组需要打开以进行http访问。

您的意思是将此文件放在/etc/httpd文件夹和has conf.d文件中吗。我找不到/etc/httpd文件夹。与conf.d相同,您使用的是哪种AMI
/etc/httpd/conf.d/
是linuxI use Ubuntu16.06gotcha上的默认apache配置目录,只是为了澄清这将在您的ec2实例上,而不是在您的个人计算机上(除非您也有apache)。请检查
/etc/apache2/
它应该有一个名为
conf.d
的子目录。这就是你想要放置自定义配置文件的地方。我从AWS得到了一个回复,提示ELB只是使用X-Forwarded-Proto插入重定向信息。因此,重定向的实际实现需要在apache的实例中进行。我需要弄清楚怎么做。类似的问题也解决了。
container_commands:
  01_update_rewrite_rules:
    command: "cp rewrite.conf /etc/httpd/conf.d/"
(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