Laravel 5 - 重定向到 HTTPS

145

我正在进行我的第一个 Laravel 5 项目,但不确定在哪里或如何放置逻辑来强制应用程序使用 HTTPS。这里的关键是有许多域名指向该应用程序,其中只有三个中的两个使用 SSL(第三个是备用域名,长话短说)。因此,我希望在我的应用程序逻辑中处理这个问题,而不是在 .htaccess 文件中处理。

在 Laravel 4.2 中,我使用以下代码在 filters.php 中实现了重定向:

App::before(function($request)
{
    if( ! Request::secure())
    {
        return Redirect::secure(Request::path());
    }
});

我认为中间件是应该实现这种功能的地方,但我无法确定如何使用它来解决问题。

谢谢!

更新

如果您像我一样使用Cloudflare,则可以通过在控制面板中添加新的页面规则来实现此操作。


第三个域名会发生什么?如果你在所有路由上强制使用https,第三个域名还能继续工作吗? - Laurence
使用 $_SERVER['HTTP_HOST'] 检测。 - NightMICU
Cloudflare页面规则生效需要多长时间? - CodeGuru
哦,我得在 DNS 设置中启用代理,哈哈! - CodeGuru
24个回答

1
这对我很有帮助。 我编写了一段自定义的PHP代码来强制将其重定向到HTTPS。 只需在header.php中包含此代码即可。
<?php
if (isset($_SERVER['HTTPS']) &&
    ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
    isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
    $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
  $protocol = 'https://';
}
else {
  $protocol = 'http://';
}
$notssl = 'http://';
if($protocol==$notssl){
    $url = "https://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";?>
    <script> 
    window.location.href ='<?php echo $url?>';
    </script> 
 <?php } ?>

0

使用.htaccess是Laravel中最简单的重定向到HTTPS的方法。

所以,您只需要将以下行添加到您的.htaccess文件中,就可以轻松实现重定向。

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

请确保在现有的(*default)代码之前添加它到.htaccess文件中,否则HTTPS将无法工作。 这是因为现有(默认)代码已经处理了一个重定向,将所有流量重定向到主页,然后根据您的URL路由接管。

因此,将代码放在首位意味着.htaccess将首先将所有流量重定向到https,然后路由接管。


0

这个在Laravel 7.x中对我有用,只需使用一个中间件就可以完成3个简单步骤

1)使用命令php artisan make:middleware ForceSSL生成中间件。

中间件

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\App;

class ForceSSL
{
    public function handle($request, Closure $next)
    {
        if (!$request->secure() && App::environment() === 'production') {
            return redirect()->secure($request->getRequestUri());
        }

        return $next($request);
    }
}

2) 在 Kernel 文件中的 routeMiddleware 中注册中间件

Kernel

protected $routeMiddleware = [
    //...
    'ssl' => \App\Http\Middleware\ForceSSL::class,
];

3) 在你的路由中使用它

路由

Route::middleware('ssl')->group(function() {
    // All your routes here

});

这里是关于中间件的完整文档

========================

.HTACCESS 方法

如果您喜欢使用 .htaccess 文件,您可以使用以下代码:

<IfModule mod_rewrite.c>
    RewriteEngine On 
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://yourdomain.com/$1 [R,L]
</IfModule>

敬礼!


0
你可以通过一个中间件类来实现它。让我给你一个想法。
namespace MyApp\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\App;

class HttpsProtocol {

    public function handle($request, Closure $next)
    {
            if (!$request->secure() && App::environment() === 'production') {
                return redirect()->secure($request->getRequestUri());
            }

            return $next($request); 
    }
}

然后,将此中间件应用于每个请求,并在Kernel.php文件中设置规则,如下所示:
protected $middleware = [
    'Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode',
    'Illuminate\Cookie\Middleware\EncryptCookies',
    'Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse',
    'Illuminate\Session\Middleware\StartSession',
    'Illuminate\View\Middleware\ShareErrorsFromSession',

    // appending custom middleware 
    'MyApp\Http\Middleware\HttpsProtocol'       

];

在上面的示例中,如果满足以下条件,中间件将会将每个请求重定向到HTTPS:
- 当前请求没有安全协议(HTTP) - 如果您的环境等于生产环境。因此,根据您的偏好调整设置即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接