强制 Laravel 使用 HTTPS 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50134427/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:41:52  来源:igfitidea点击:

Force Laravel to use HTTPS version

phplaravelsslhttpspagination

提问by Avi

I have a laravel site and I'm trying to get everything to work on with SSL. Though it seems that Laravel wants to use http instead. For example I was using the formelement and the method was automatically sending to http. The same thing using the built in style, scriptlinks which I changed all to work statically (regular ol' HTML). The last thing which I can't manage to figure out is the pagination (using jscroll). I have this row:

我有一个 Laravel 站点,我正在尝试使用 SSL 处理所有内容。尽管 Laravel 似乎想改用 http。例如,我正在使用该form元素,并且该方法会自动发送到 http。同样的事情使用内置的style,script链接,我将其全部更改为静态工作(常规 ol' HTML)。我无法弄清楚的最后一件事是分页(使用 jscroll)。我有这一行:

{!! $plugins->appends(['tag' => $param2, 'search' => $param1 ])->render() !!}

Which prints this:

打印这个:

    <ul class="pager">
<li class="disabled"><span>?</span></li> 
<li class="jscroll-next-parent" style="display: none;"><a href="http://siteurl.net/pagename/page/?tag=XXX&amp;search=&amp;page=2" rel="next">?</a>
</li>
</ul>

Any idea how I change that to be https? Thanks!

知道我如何将其更改为 https 吗?谢谢!

回答by Adam Kozlowski

Try this, go to AppServiceProvider place this code in the boot method:

试试这个,去 AppServiceProvider 把这段代码放在 boot 方法中:

\URL::forceScheme('https');

The class:

班上:

namespace App\Providers;

use Illuminate\Support\Facades\URL;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        URL::forceScheme('https');
    }
}

Other way is enforce by .htaccess:

其他方式由 .htaccess 强制执行:

RewriteEngine On

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

Remember about clearing cache in browser (then htaccess will work correctly).

请记住清除浏览器中的缓存(然后 htaccess 将正常工作)。

Good luck!

祝你好运!

回答by Luca C.

If you want to generate https routes only (but still plain http requests allowed), change AppServiceProvider:

如果您只想生成 https 路由(但仍然允许普通的 http 请求),请更改 AppServiceProvider:

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        URL::forceScheme('https');
    }
}

if you want to redirect any http request to https, add a global middleware:

如果要将任何 http 请求重定向到 https,请添加全局中间件:

class ForceHttpsMiddleware{
    public function handle($request, Closure $next){

        if (!$request->secure() && App::environment() === 'production') {//optionally disable for localhost development
            return redirect()->secure($request->getRequestUri());
        }

        return $next($request);
    }
}

回答by Designer Developer

forceScheme is wrong it should be forceSchema, you should find AppServiceProvider and under the boot function add:

forceScheme 错了应该是forceSchema,你应该找到AppServiceProvider并在boot函数下添加:

\URL::forceScheme('https');

It means it will be

这意味着它将是

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

回答by Avi

I got everything to work by enforcing SSL on Couldflare. It's not in the code but it works.

我通过在 Canflare 上强制执行 SSL 使一切正常。它不在代码中,但它有效。