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



把我的头发。我需要强制使用 HTTPS 并在 .htaccess 文件中使用以下内容获得太多重定向。根据我对 Stack 的研究,这应该有效。清除缓存,饼干,所有这些。

<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>

好的,想通了。因为我在负载均衡器后面,所以我不得不替换:

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]

有关为什么需要该标头的详细信息,请参阅:负载均衡器和 HTTPS

我遇到了类似的问题(使用 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

我在共享主机上使用 Laravel 6 时遇到了类似的问题。当我配置 CPanel 将我的应用程序重定向到安全的httpsURL 时,它会无限地重定向。

.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]

它解决了它。

在 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]  

最新更新