18

today I decided to move my website to HTTPS. Early my website work on HTTP.

My problem is in misunderstood how Laravel pass HTTP and https in helpers function route('name')

I change my website URL in config/app.php to https://www.domain.name and I think, this solution helps me. But I got a strange result.

In php artisan tinker if I pass route('ROUTE.NAME') I got right link https://www.domain.name/route/path but in blade template I got http://www.domain.name/route/path

The same situation with \URL::to('/')

Maybe someone can explain to me why this happened?

4
  • What is written exactly in your config/app.php? Could it be that another url is defined in the .env file in the root and env() is loading it? Commented Jun 7, 2017 at 10:22
  • Are you visiting the site in the browser using http or https? Commented Jun 7, 2017 at 10:25
  • @UfguFugullu in .env I pass domain.name and in config/app.php too Commented Jun 7, 2017 at 10:26
  • @DovBenyominSohacheski I redirect users via NGINX to https www by default Commented Jun 7, 2017 at 10:27

5 Answers 5

37

The @dekts response is correct, but the "right" place to put this kind of stuff is the "app/Providers/AppServiceProvider.php" on the boot method.

//file: app/Providers/AppServiceProvider.php use Illuminate\Support\Facades\URL; class AppServiceProvider extends ServiceProvider { public function boot() { if (app()->environment('remote')) { URL::forceScheme('https'); } } ... } 

You can also add a new variable on your ".env" file, something like:

#file: .env FORCE_HTTPS=true 

And change the condition to

//file: app/Providers/AppServiceProvider.php public function boot() { if(env('FORCE_HTTPS',false)) { // Default value should be false for local server URL::forceScheme('https'); } } 

Hope this help.

edit: corrected forceSchema to forceScheme as noted on the comments. ;)

Sign up to request clarification or add additional context in comments.

4 Comments

should be: URL::forceScheme('https');
use Illuminate\Support\Facades\URL; is not needed if you use url()->forceSchema('https');
Scheme not Schema
URL::forceScheme('https'); still works on Laravel 7.
8

I struggled with this for ages. My solution was to put the following in my routes.php file (you may prefer a different place for it)

I have also wrapped a conditional statement around this line, so it only applies to my remote configuration:

if (App::environment('remote')) { URL::forceSchema('https'); } 

You can try to add in your AppServiceProvider in the boot method. here: app/Providers/AppServiceProvider.php

Or another way:

So I figured it out. It not that easy but in your AppServiceProvider.php you must add: $this->app['request']->server->set('HTTPS', true); in register method. And that is how I figured it out.

Btw. Setting APP_URL does nothing to HTTP side of your app, it's for artisan.

PS. CloudFlare HTTPS Rewrites are also good idea :)

Comments

7

in .env file

FORCE_HTTPS=true 

in app/Providers/AppServiceProvider.php

public function boot() { if(env('FORCE_HTTPS',false)) { URL::forceScheme('https'); } } 

After 5.4 the method name is forceScheme

if you use 'forceSchema' will get an error

"Method Illuminate\Routing\UrlGenerator::forceSchema does not exist." 

Comments

6

Automatic detection http/https

<?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\URL; use Illuminate\Support\Facades\Request; class AppServiceProvider extends ServiceProvider { public function boot() { /* Use https links instead http links */ if (Request::server('HTTP_X_FORWARDED_PROTO') == 'https') { URL::forceScheme('https'); } } } 

Comments

2

.env file

FORCE_HTTPS=true 

app/Providers/AppServiceProvider.php

use Illuminate\Support\Facades\URL; 
if (app()->environment('remote') || env('FORCE_HTTPS',false)) { URL::forceScheme('https'); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.