在 laravel 5.2 中将 http 重定向到 https
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44010460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect http to https in laravel 5.2
提问by Juan Lopez
I have a Laravel 5.2 project. Recently, I installed an SSL certificate on my website so when I type https://example.com
in the browser it works but when I write example.com
, it connects by HTTP.
我有一个 Laravel 5.2 项目。最近,我在我的网站上安装了一个 SSL 证书,所以当我https://example.com
在浏览器中输入时它可以工作,但是当我写时example.com
,它通过 HTTP 连接。
To fix that, I added these lines to my .htaccess
file:
为了解决这个问题,我将这些行添加到我的.htaccess
文件中:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
After that, when I type example.com
, the browser redirects me to https://example.com
. That's perfect.
之后,当我输入时example.com
,浏览器会将我重定向到https://example.com
. 那很完美。
The problem is when I try to access the URL http://example.com/download/get/book/volume2
, it doesn't redirect me to HTTPS. It redirects me to the index page. If I add the s
of https
in the URL it works fine but I need the app to redirect me from HTTP to HTTPS.
问题是当我尝试访问 URL 时http://example.com/download/get/book/volume2
,它不会将我重定向到 HTTPS。它将我重定向到索引页面。如果我在 URL 中添加s
ofhttps
它工作正常,但我需要应用程序将我从 HTTP 重定向到 HTTPS。
The route in routes file:
路由文件中的路由:
Route::get('download/get/book/{book}'
That route opens a PDF file that I have in privates/storage
folder in the browser's tab.
该路线会打开一个 PDF 文件,该privates/storage
文件位于浏览器选项卡的文件夹中。
EDIT:
编辑:
My .htaccess file:
我的 .htaccess 文件:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [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}]
# require SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
回答by MrWhite
Your HTTP to HTTPS redirect needs to go near the top of your .htaccess
file, immediately after the RewriteEngine
directive and, importantly, beforethe Laravel front controller.
您的 HTTP 到 HTTPS 重定向需要靠近.htaccess
文件顶部,紧跟在RewriteEngine
指令之后,重要的是,在Laravel前端控制器之前。
As a general rule, external redirectsshould always go before internal rewrites.
作为一般规则,外部重定向应该始终在内部重写之前进行。
The front controller catches all requests(or rather requests that don't map to a physical file or directory) and rewrites these to index.php
(that ultimately handles the routeing). The problem is that your HTTP to HTTPS redirect occurs later and converts the now rewritten URL into an external redirect to index.php
.
前端控制器捕获所有请求(或者更确切地说是未映射到物理文件或目录的请求)并将它们重写为index.php
(最终处理路由)。问题是您的 HTTP 到 HTTPS 重定向稍后发生,并将现在重写的 URL 转换为外部重定向到index.php
.
Likewise, your # Handle Authorization Header
block should also go before the front controller(ideally). (It will work in its current state, in per-directory .htaccess
files, due to the looping nature of .htaccess
files, but if you moved these directives to the server config it would fail.)
同样,您的# Handle Authorization Header
块也应该在前端控制器之前(理想情况下)。(.htaccess
由于文件的循环性质,它将在当前状态下在每个目录.htaccess
文件中工作,但是如果您将这些指令移动到服务器配置,它将失败。)
回答by NIKHIL NEDIYODATH
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Add the above two lines just below RewriteEngine On
in your .htaccess
file. It will automatically redirect any traffic destined for http: to https:
RewriteEngine On
在您的.htaccess
文件中添加上面两行。它会自动将所有发往 http: 的流量重定向到 https:
Finally, your .htaccess file will look like the following
最后,您的 .htaccess 文件将如下所示
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Redirect to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>