【问题标题】:Redirect https with laravel使用 laravel 重定向 https
【发布时间】:2021-12-02 10:50:00
【问题描述】:

我建立了一个网站,我想将 http(本地)重定向到 https(在线使用)。我尝试使用中间件,使用AppServiceProvider.htacessRouteServiceProvider,但没有成功。

1、当我使用中间件时

class HttpsProtocol
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle(Request $request, Closure $next)
    {
        if (!app()->environment('local')) {
            // for Proxies
            Request::setTrustedProxies([$request->getClientIp()], 
                Request::HEADER_X_FORWARDED_ALL);

            if (!$request->isSecure()) {
                return redirect()->secure($request->path(), 301);
            }
        }

        return $next($request);
    
    }
}

2,当我使用.htancess时

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>


    RewriteEngine On

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

    # 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]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
3、当我使用 AppServiceProvider 时

public function boot()
    {
        if (!app()->environment('local')) {
            $this->app['request']->server->set('HTTPS', true);
            URL::forceScheme('https');
        }
        //
    }
使用 get 方法时 https://i.stack.imgur.com/XXiRG.png

https://i.stack.imgur.com/4tZz0.png

它创建了另一个 https 链接并且可以运行。 但是当我使用自动检查蛞蝓时

https://i.stack.imgur.com/Kv8rh.png

当我使用 Post 方法时

https://i.stack.imgur.com/9vKOD.png

https://i.stack.imgur.com/rAoMD.png 它没有进入https, 请帮我 ! (对不起,因为我的英语)

【问题讨论】:

  • 我们不调试图像!
  • 首先,您需要修复图像。然后,您需要提供有关您在 MiddlewareAppServiceProvider.htaccessRouteServiceProvider 上尝试的内容的更多信息。
  • 好的,感谢您的反馈,我已经添加了代码

标签: php laravel laravel-routing


【解决方案1】:

只需打开 AppServiceProvider 并添加

\URL::forceScheme('https'); 

在启动功能中。就是这样。

    public function boot()
    {
        \URL::forceScheme('https');
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-08
    • 2023-03-24
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多