Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force Laravel to use HTTPS version

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 form element and the method was automatically sending to http. The same thing using the built in style, script links 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:

{!! $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!

like image 472
Avi Avatar asked May 02 '18 12:05

Avi


2 Answers

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

\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:

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).

Good luck!

like image 89
Adam Kozlowski Avatar answered Oct 17 '22 22:10

Adam Kozlowski


If you want to generate https routes only (but still plain http requests allowed), change 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:

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);
    }
}
like image 39
Luca C. Avatar answered Oct 17 '22 22:10

Luca C.