如何强制 Laravel 项目对所有路由使用 HTTPS?
How to force Laravel Project to use HTTPS for all routes?
我正在从事一个需要安全连接的项目。
我可以通过以下方式设置要使用的路线、uri、资产 'https':
Route::get('order/details/{id}', ['uses' => 'OrderController@details', 'as' => 'order.details', 'https']);
url($language.'/index', [], true)
asset('css/bootstrap.min.css', true)
但是一直设置参数好像很累
有没有办法强制所有路由生成 HTTPS 链接?
您可以在 config/app.php
中设置 'url' => 'https://youDomain.com'
或者您可以使用中间件 class .
将此添加到您的 .htaccess 代码中
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/ [R,L]
将 www.yourdomain.com 替换为您的域名。这将强制您域的所有网址都使用 https。确保在您的域上安装并配置了 https 证书。如果您没有看到绿色的 https 是安全的,请在 chrome 上按 f12 并修复控制台选项卡中的所有混合错误。
希望对您有所帮助!
这里有几种方法。选择最方便的。
配置您的 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;
}
使用 https:
设置环境变量 APP_URL
APP_URL=https://example.com
使用助手secure_url() (Laravel5.6)
将以下字符串添加到 AppServiceProvider::boot() 方法(对于版本 5.4+):
\Illuminate\Support\Facades\URL::forceScheme('https');
更新:
路由组的隐式设置方案(Laravel5.6):
Route::group(['scheme' => 'https'], function () {
// Route::get(...)->name(...);
});
我在 web.php
或 api.php
文件的末尾使用了它,它运行良好:
URL::forceScheme('https');
将它放在 AppServiceProvider 的 boot() 方法中
if($this->app->environment('production')) {
\URL::forceScheme('https');
}
在您的 .htaccess 文件中使用以下代码会自动将访问者重定向到您网站的 HTTPS 版本:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
试试这个 - 它会起作用
在 RouteServiceProvider 文件中
$url = \Request::url();
$check = strstr($url,"http://");
if($check)
{
$newUrl = str_replace("http","https",$url);
header("Location:".$newUrl);
}
public function boot()
{
if(config('app.debug')!=true) {
\URL::forceScheme('https');
}
}
在 app/Providers/AppServiceProvider.php
我宁愿 forceScheme
而不是在网络服务器上进行。所以Laravel应用程序应该对此负责。
所以正确的方法是在 app/Providers/AppServiceProvider.php
的 boot
函数中添加 if 语句
if (env('APP_ENV') === 'production') {
\Illuminate\Support\Facades\URL::forceScheme('https');
}
提示:证明您APP_ENV配置正确。转到您的 Linux 服务器,输入 env
这是在 Laravel 5 上测试的,特别是 5.6。
在 Laravel 7.x (2020)
中强制使用 Https
"2020 Update? Url::forceScheme was acting funky for me, but this worked liked a dime."
https
代码片段.
解决(\Illuminate\Routing\UrlGenerator::class)->forceScheme('https');
- 在任何服务提供商引导方法中添加该片段
- 1:打开
app/providers/RouteServiceProvider.php
。
- 2:然后将https代码片段添加到boot方法中。
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
resolve(\Illuminate\Routing\UrlGenerator::class)->forceScheme('https');
parent::boot();
}
- 3: 最后 运行
php artisan route:clear && composer dumpautoload
清除Laravel的缓存路由和缓存服务提供商。
在您的 .env 文件中,只需使用
FORCE_HTTPS=true
这对我有用,您也可以将 APP Url 设置为 https://your-site.com 作为附加步骤
我想出了如何在负载平衡的基础架构中执行此操作。
你需要在你的Nginx配置中添加:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
add_header X-Forwarded-Proto https;
add_header X-Forwarded-Port 443;
add_header Ssl-Offloaded "1";
add_header Access-Control-Allow-Origin "*";
fastcgi_param HTTPS "on";
fastcgi_param HTTP_X_FORWARDED_PROTO "https";
}
导入器部分是 add_headers 和 pastcgi_params,它通过 AWS 负载均衡器工作得很好。
解决此问题的更好方法是:
-> 转到 public 文件夹,
->编辑.htaccess
只需在下面添加此代码:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
-> 并保存。
-> 关闭并重新打开浏览器。
对于laravel 8,如果您尝试了上述所有方法但浏览器重定向次数过多错误,请在TrustProxies
中间件中设置代理,如下所示:
App\Http\Middleware\TrustProxies.php
/**
* The trusted proxies for this application.
*
* @var array|string|null
*/
protected $proxies = '*';
在文件的 class 名称前加上这个 app\Providers\AppServiceProvider.php
use Illuminate\Support\Facades\URL;
然后将这段代码粘贴到app\Providers\AppServiceProvider.php文件
的启动函数中
if (config('app.env') === 'production') {
URL::forceScheme('https');
}
这是另一个选项,适用于 laravel 8.*。不过我不确定低版本:
将 ASSET_URL
变量添加到您的 .env
文件。
例如:
ASSET_URL=https://secure-domain.com
您可以在此处找到更多信息:Laravel Helpers
提示:注意上面link中的注释
只用.htaccess文件实现https重定向怎么样?这个应该放在project root
(not in public folder)
中。您的服务器需要配置为指向项目根目录。
<IfModule mod_rewrite.c>
RewriteEngine On
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove public folder form URL
RewriteRule ^(.*)$ public/ [L]
</IfModule>
我正在使用 Apache 服务器,我认为最有效的方法是更改虚拟主机配置。
改成这样
<VirtualHost *:80>
ServerName www.yourdomain.com
Redirect / https://www.yourdomain.com
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.yourdomain.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
使用 secure_url() 来自 laravel 5.2
的函数
$url = secure_url('user/profile');
{{ secure_url('your-link') }} //instead url()
我正在从事一个需要安全连接的项目。
我可以通过以下方式设置要使用的路线、uri、资产 'https':
Route::get('order/details/{id}', ['uses' => 'OrderController@details', 'as' => 'order.details', 'https']);
url($language.'/index', [], true)
asset('css/bootstrap.min.css', true)
但是一直设置参数好像很累
有没有办法强制所有路由生成 HTTPS 链接?
您可以在 config/app.php
中设置 'url' => 'https://youDomain.com'
或者您可以使用中间件 class
将此添加到您的 .htaccess 代码中
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/ [R,L]
将 www.yourdomain.com 替换为您的域名。这将强制您域的所有网址都使用 https。确保在您的域上安装并配置了 https 证书。如果您没有看到绿色的 https 是安全的,请在 chrome 上按 f12 并修复控制台选项卡中的所有混合错误。
希望对您有所帮助!
这里有几种方法。选择最方便的。
配置您的 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; }
使用 https:
设置环境变量APP_URL
APP_URL=https://example.com
使用助手secure_url() (Laravel5.6)
将以下字符串添加到 AppServiceProvider::boot() 方法(对于版本 5.4+):
\Illuminate\Support\Facades\URL::forceScheme('https');
更新:
路由组的隐式设置方案(Laravel5.6):
Route::group(['scheme' => 'https'], function () { // Route::get(...)->name(...); });
我在 web.php
或 api.php
文件的末尾使用了它,它运行良好:
URL::forceScheme('https');
将它放在 AppServiceProvider 的 boot() 方法中
if($this->app->environment('production')) {
\URL::forceScheme('https');
}
在您的 .htaccess 文件中使用以下代码会自动将访问者重定向到您网站的 HTTPS 版本:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
试试这个 - 它会起作用 在 RouteServiceProvider 文件中
$url = \Request::url();
$check = strstr($url,"http://");
if($check)
{
$newUrl = str_replace("http","https",$url);
header("Location:".$newUrl);
}
public function boot()
{
if(config('app.debug')!=true) {
\URL::forceScheme('https');
}
}
在 app/Providers/AppServiceProvider.php
我宁愿 forceScheme
而不是在网络服务器上进行。所以Laravel应用程序应该对此负责。
所以正确的方法是在 app/Providers/AppServiceProvider.php
boot
函数中添加 if 语句
if (env('APP_ENV') === 'production') {
\Illuminate\Support\Facades\URL::forceScheme('https');
}
提示:证明您APP_ENV配置正确。转到您的 Linux 服务器,输入 env
这是在 Laravel 5 上测试的,特别是 5.6。
在 Laravel 7.x (2020)
中强制使用 Https"2020 Update? Url::forceScheme was acting funky for me, but this worked liked a dime."
https
代码片段.解决(\Illuminate\Routing\UrlGenerator::class)->forceScheme('https');
- 在任何服务提供商引导方法中添加该片段
- 1:打开
app/providers/RouteServiceProvider.php
。 - 2:然后将https代码片段添加到boot方法中。
/**
* Define your route model bindings, pattern filters, etc.
*
* @return void
*/
public function boot()
{
resolve(\Illuminate\Routing\UrlGenerator::class)->forceScheme('https');
parent::boot();
}
- 3: 最后 运行
php artisan route:clear && composer dumpautoload
清除Laravel的缓存路由和缓存服务提供商。
在您的 .env 文件中,只需使用
FORCE_HTTPS=true
这对我有用,您也可以将 APP Url 设置为 https://your-site.com 作为附加步骤
我想出了如何在负载平衡的基础架构中执行此操作。
你需要在你的Nginx配置中添加:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
add_header X-Forwarded-Proto https;
add_header X-Forwarded-Port 443;
add_header Ssl-Offloaded "1";
add_header Access-Control-Allow-Origin "*";
fastcgi_param HTTPS "on";
fastcgi_param HTTP_X_FORWARDED_PROTO "https";
}
导入器部分是 add_headers 和 pastcgi_params,它通过 AWS 负载均衡器工作得很好。
解决此问题的更好方法是: -> 转到 public 文件夹, ->编辑.htaccess 只需在下面添加此代码:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
-> 并保存。 -> 关闭并重新打开浏览器。
对于laravel 8,如果您尝试了上述所有方法但浏览器重定向次数过多错误,请在TrustProxies
中间件中设置代理,如下所示:
App\Http\Middleware\TrustProxies.php
/**
* The trusted proxies for this application.
*
* @var array|string|null
*/
protected $proxies = '*';
在文件的 class 名称前加上这个 app\Providers\AppServiceProvider.php
use Illuminate\Support\Facades\URL;
然后将这段代码粘贴到app\Providers\AppServiceProvider.php文件
的启动函数中if (config('app.env') === 'production') {
URL::forceScheme('https');
}
这是另一个选项,适用于 laravel 8.*。不过我不确定低版本:
将 ASSET_URL
变量添加到您的 .env
文件。
例如:
ASSET_URL=https://secure-domain.com
您可以在此处找到更多信息:Laravel Helpers
提示:注意上面link中的注释
只用.htaccess文件实现https重定向怎么样?这个应该放在project root
(not in public folder)
中。您的服务器需要配置为指向项目根目录。
<IfModule mod_rewrite.c>
RewriteEngine On
# Force SSL
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Remove public folder form URL
RewriteRule ^(.*)$ public/ [L]
</IfModule>
我正在使用 Apache 服务器,我认为最有效的方法是更改虚拟主机配置。 改成这样
<VirtualHost *:80>
ServerName www.yourdomain.com
Redirect / https://www.yourdomain.com
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.yourdomain.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
使用 secure_url() 来自 laravel 5.2
的函数$url = secure_url('user/profile');
{{ secure_url('your-link') }} //instead url()