2

I have a shared hosting with OVH(France) and i have the "Let's Encrypt" certificate for my domain.

however, i looked everywhere for redirecting all requests from HTTP to HTTPS in laravel 5.4

i have found a solution with ".htacces redirecting" but i often have "TOO_MANY_REDIRECT" errors on browsers specially Google Chrome.

Anyone have an idea for redirecting all PS : i don't have "sudo" rights on my shared hosting server (just user access with ssh)

Regards,

aymanesoft
  • 23
  • 1
  • 4
  • Possible duplicate of [htaccess redirect to https://www](https://stackoverflow.com/questions/13977851/htaccess-redirect-to-https-www) – Alex Slipknot May 22 '17 at 13:15

4 Answers4

1

Without modify the .htaccess file, you can force the https protocol in your Laravel application adding:

function boot() {
     URL::forceScheme('https');
     ... your code
}

In your AppServiceProvider.php.

Troyer
  • 6,765
  • 3
  • 34
  • 62
  • Thank you @Troyer, i added "use Illuminate\Support\Facades\URL;" in AppServiceProvider and it's works However, if i try to access to my web site by removing "https://" it's not redirecting me to https. Redirecting to https works only if i click on a link in my web site to go on login or about page. Is there any way to force redirecting from http to https (i dont want http anymore) Thank you in advance for your answer – aymanesoft May 22 '17 at 20:24
  • @aymanesoft Found a very good answer that fits your problem: https://stackoverflow.com/questions/28402726/laravel-5-redirect-to-https – Troyer May 23 '17 at 07:03
0

You can set 'url' => env('APP_URL', 'https://localhost'), in config/app.php. That should do the trick.

0

in addition to @Troyer answer, i added the code below to my .htacces

RewriteEngine on

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]

and now all request to HTTP are redirected to HTTPS without the "TOO_MANY_REDIRECT" errors thank you very much guys for your answers best regards,

aymanesoft
  • 23
  • 1
  • 4
0

If you'd like to force HTTPS over all URLs of your app without having to change your Apache or Nginx configuration, you need to update your AppServiceProvider as followed:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        if (env('APP_ENV') === 'production') {
            $this->app['url']->forceScheme('https');
        }
    }
}

PS: you can remove the condition if you have SSL enabled on your local development environment.

Waiyl Karim
  • 2,810
  • 21
  • 25