php - Laravel 将 Http 重定向到 Https

标签 php laravel http https

我们使用 Laravel 5。要将 http 连接重定向到 https,请使用中间件 HttpsProtocol。

namespace MyApp\Http\Middleware;

use Closure;

class HttpsProtocol {

    public function handle($request, Closure $next)
    {
            if (!$request->secure() && env('APP_ENV') === 'prod') {
                return redirect()->secure($request->getRequestUri());
            }

            return $next($request); 
    }
}

在我们的 4 个测试用例中,只有 1 个(最后一次重定向)正确工作。其他3个案例中间件添加url extra index.php。

http://www.aqualink.az/index.php ---> https://www.aqualink.az/index.php/index.php http://aqualink.az/index.php ---> https://aqualink.az/index.php/index.php https://www.aqualink.az/index.php ---> https://www.aqualink.az/index.php/index.php https://aqualink.az/index.php ---> https://aqualink.az/index.php

最佳答案

我用htaccess解决了

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

关于php - Laravel 将 Http 重定向到 Https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39249334/

相关文章:

http - Flutter:单例数据,getter 在 null 上调用

php - Codeigniter 应用程序可以在一个子域中运行,但不能在另一个子域中运行。为什么?

javascript - 一页上多个表单,使用ajax发布数据,指定所选表单中的哪些输入字段

mysql - 在 laravel 中发送 ajax 后,laravel 在自定义文件中查询?

javascript - Vue js "Cannot read property ' get' of undefined“在获取请求时

http - 将标签添加到默认的 Golang Prometheus 指标

php - 在 Grocery Crud 库 Codeigniter 中阅读作品、添加、编辑、删除

php - 使用 WP_Comment_Query 获取评论

laravel - 尝试安装 gloudemans/shoppingcart,安装失败,将 ./composer.json 恢复为其原始内容?

http - 格式异常 : Invalid radix-10 number