php 如何强制 Laravel 项目对所有路由使用 HTTPS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35827062/
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
How to force Laravel Project to use HTTPS for all routes?
提问by Nelson Melecio
I am working on a project that requires a secure connection.
我正在做一个需要安全连接的项目。
I can set the route, uri, asset to use 'https' via:
我可以通过以下方式设置路由、uri、资产以使用“https”:
Route::get('order/details/{id}', ['uses' => 'OrderController@details', 'as' => 'order.details', 'https']);
url($language.'/index', [], true)
asset('css/bootstrap.min.css', true)
But setting the parameters all the time seems tiring.
但是一直设置参数似乎很累。
Is there a way to force all routes to generate HTTPS links?
有没有办法强制所有路由生成HTTPS链接?
采纳答案by Mirceac21
You can set 'url' => 'https://youDomain.com'
in config/app.php
or you could use a middleware class Laravel 5 - redirect to HTTPS.
您可以设置'url' => 'https://youDomain.com'
在config/app.php
或者你可以使用一个中间件类Laravel 5 -重定向到HTTPS。
回答by Prisacari Dmitrii
Here are several ways. Choose most convenient.
这里有几种方法。选择最方便。
Configure your web server to redirect all non-secure requests to https. Example of a nginx config:
server { listen 80 default_server; listen [::]:80 default_server; server_name example.com www.example.com; return 301 https://example.com$request_uri; }
Set your environment variable
APP_URL
using https:APP_URL=https://example.com
Use helper secure_url()(Laravel5.6)
Add following string to AppServiceProvider::boot() method (for version 5.4+):
\Illuminate\Support\Facades\URL::forceScheme('https');
配置您的 Web 服务器以将所有非安全请求重定向到 https。nginx 配置示例:
server { listen 80 default_server; listen [::]:80 default_server; server_name example.com www.example.com; return 301 https://example.com$request_uri; }
APP_URL
使用 https设置环境变量:APP_URL=https://example.com
使用助手secure_url()(Laravel5.6)
将以下字符串添加到 AppServiceProvider::boot() 方法(适用于 5.4+ 版本):
\Illuminate\Support\Facades\URL::forceScheme('https');
Update:
更新:
Implicitly setting scheme for route group (Laravel5.6):
Route::group(['scheme' => 'https'], function () { // Route::get(...)->name(...); });
路由组隐式设置方案(Laravel5.6):
Route::group(['scheme' => 'https'], function () { // Route::get(...)->name(...); });
回答by Amitesh
Place this in the AppServiceProvider in the boot() method
将此放在 AppServiceProvider 的 boot() 方法中
if($this->app->environment('production')) {
\URL::forceScheme('https');
}
回答by lomelisan
I used this at the end of the web.php
or api.php
file and it worked perfectly:
我在web.php
orapi.php
文件的末尾使用了它,它工作得很好:
URL::forceScheme('https');
回答by Srikanth Gopi
Add this to your .htaccess code
将此添加到您的 .htaccess 代码中
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/ [R,L]
Replace www.yourdomain.com with your domain name. This will force all the urls of your domain to use https. Make sure you have https certificate installed and configured on your domain. If you do not see https in green as secure, press f12 on chrome and fix all the mixed errors in the console tab.
将 www.yourdomain.com 替换为您的域名。这将强制您域的所有 url 使用 https。确保您的域上安装并配置了 https 证书。如果您没有看到绿色的 https 是安全的,请在 chrome 上按 f12 并修复控制台选项卡中的所有混合错误。
Hope this helps!
希望这可以帮助!
回答by arian
Using the following code in your .htaccess file automatically redirects visitors to the HTTPS version of your site:
在 .htaccess 文件中使用以下代码会自动将访问者重定向到您网站的 HTTPS 版本:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
回答by Markos F
public function boot()
{
if(config('app.debug')!=true) {
\URL::forceScheme('https');
}
}
in app/Providers/AppServiceProvider.php
在 app/Providers/AppServiceProvider.php
回答by laimison
I would prefer forceScheme
instead of doing it on a web server. So Laravel app should be responsible for it.
我宁愿forceScheme
而不是在网络服务器上做。所以 Laravel 应用程序应该负责它。
So right way is to add if statement inside boot
function in your app/Providers/AppServiceProvider.php
所以正确的方法是boot
在你的函数中添加 if 语句app/Providers/AppServiceProvider.php
if (env('APP_ENV') === 'production') {
\Illuminate\Support\Facades\URL::forceScheme('https');
}
Tip: to prove that you have APP_ENV configured correctly. Go to your Linux server, type env
提示:证明你已经正确配置了APP_ENV。转到您的 Linux 服务器,键入env
This was tested on Laravel 5, specifically 5.6.
这是在 Laravel 5,特别是 5.6 上测试的。
回答by mostafa nahfouz
try this - it will work in RouteServiceProvider file
试试这个 - 它会在 RouteServiceProvider 文件中工作
$url = \Request::url();
$check = strstr($url,"http://");
if($check)
{
$newUrl = str_replace("http","https",$url);
header("Location:".$newUrl);
}
回答by Cosmo Arun
add this hardcore 302 redirect to the end of public/.htaccess file,
将此硬核 302 重定向添加到 public/.htaccess 文件的末尾,
RewriteEngine on
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} (.*)\.yoursite\.com$
RewriteCond %{HTTP_HOST} yoursite\.com$
RewriteCond %{HTTP_HOST} ^yoursite\.com [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.yoursite\.com$
RewriteRule ^(.*) https://www.yoursite.com/%1/ [L,R=302]