Laravel使用Amazon ELB强制使用.htaccess进行HTTPS重定向过多

时间:2018-09-05 11:38:50

标签: laravel .htaccess

拔出我的头发。我需要强制使用HTTPS并在.htaccess文件中使用以下内容进行过多的重定向。根据我对Stack的研究,这应该可行。清除缓存,cookie等等。

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

4 个答案:

答案 0 :(得分:3)

好,知道了。因为我在负载均衡器后面,所以不得不更换:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

其中:

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

有关为何在此处需要该标题的更多信息:Load Balancers and HTTPS

答案 1 :(得分:0)

我有一个类似的问题(使用Codeigniter并使用AWS Classic ELB),但是上述解决方案对我不起作用。我必须在https.conf文件中应用重写条件和规则,然后重新启动apache服务器。请参阅此处以了解操作方法。同样值得注意的是,AWS不建议使用.htaccess方法(不确定原因):

https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb/

https://www.youtube.com/watch?v=hvqZV_50GlQ

答案 2 :(得分:0)

我在共享主机上遇到了Laravel 6的类似问题。当我将CPanel配置为将我的应用重定向到安全的https网址时,它会一直无限重定向。

我在.htaccess中有这个

RewriteCond %{HTTP_HOST} ^biblioteca\.vallenateca\.com$ [OR]   
RewriteCond %{HTTP_HOST} ^www\.biblioteca\.vallenateca\.com$
RewriteRule ^/?$ "https\:\/\/biblioteca\.vallenateca\.com" [R=301,L]

我添加了这两行。

RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteCond %{HTTP_HOST} ^biblioteca\.vallenateca\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.biblioteca\.vallenateca\.com$
RewriteRule ^/?$ "https\:\/\/biblioteca\.vallenateca\.com" [R=301,L]

它解决了。

答案 3 :(得分:0)

在 laravel 8 中,.htaccess 文件的默认值是:

RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

这对我有用,我正在使用 Inmotion 托管:

RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]  
相关问题