6

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!

Avi
  • 728
  • 3
  • 10
  • 20

5 Answers5

18

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!

Adam Kozlowski
  • 5,606
  • 2
  • 32
  • 51
1

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);
    }
}
Luca C.
  • 11,714
  • 1
  • 86
  • 77
0

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

Avi
  • 728
  • 3
  • 10
  • 20
0

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

\URL::forceScheme('https');

It means it will be

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

open app\Providers\AppServiceProvider.php

add below class

use Illuminate\Support\Facades\URL; 

then just write below source in Boot() function of AppServiceProvider.php

<?php

if($this->app->environment('production')) { // it will force redirect to https in Production mode 
      URL::forceScheme('https');
}

?>

it will force redirect http request to https

& then update APP_URL variable with https url in .env file

for ex:-

APP_URL=https://stackoverflow.com

If you use this way then you does not required to any change in server file or .htaccess file

Note:- I use Laravel 8 So, I am not sure it will works in below Version of Laravel 8

Harsh Patel
  • 1,032
  • 6
  • 21