How to force Laravel Project to use HTTPS for all routes?

前端 未结 10 497
攒了一身酷
攒了一身酷 2020-12-13 03:33

I am working on a project that requires a secure connection.

I can set the route, uri, asset to use \'https\' via:

Route::get(\'order/details/{id}\',         


        
相关标签:
10条回答
  • 2020-12-13 04:13

    Force Https in Laravel 7.x (2020)


    "2020 Update? Url::forceScheme was acting funky for me, but this worked liked a dime."


    1. https code snippet.

      resolve(\Illuminate\Routing\UrlGenerator::class)->forceScheme('https');


    1. Add that snippet within any Service Provider Boot Method

    • 1: Open app/providers/RouteServiceProvider.php.
    • 2: Then add the https code snippet to the boot method.
        /**
         * Define your route model bindings, pattern filters, etc.
         *
         * @return void
         */
        public function boot()
        {
            resolve(\Illuminate\Routing\UrlGenerator::class)->forceScheme('https');
    
            parent::boot();
        }
    
    • 3: Lastly run php artisan route:clear && composer dumpautoload to clear Laravel's cached routes and cached Service Providers.
    0 讨论(0)
  • 2020-12-13 04:14

    Using the following code in your .htaccess file automatically redirects visitors to the HTTPS version of your site:

    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    0 讨论(0)
  • 2020-12-13 04:14

    I would prefer forceScheme instead of doing it on a web server. So Laravel app should be responsible for it.

    So right way is to add if statement inside boot function in your app/Providers/AppServiceProvider.php

        if (env('APP_ENV') === 'production') {
            \Illuminate\Support\Facades\URL::forceScheme('https');
        }
    

    Tip: to prove that you have APP_ENV configured correctly. Go to your Linux server, type env

    This was tested on Laravel 5, specifically 5.6.

    0 讨论(0)
  • 2020-12-13 04:23

    Here are several ways. Choose most convenient.

    1. Configure your web server to redirect all non-secure requests to https. Example of a nginx config:

      server {
          listen 80 default_server;
          listen [::]:80 default_server;
          server_name example.com www.example.com;
          return 301 https://example.com$request_uri;
      }
      
    2. Set your environment variable APP_URL using https:

      APP_URL=https://example.com
      
    3. Use helper secure_url() (Laravel5.6)

    4. Add following string to AppServiceProvider::boot() method (for version 5.4+):

      \Illuminate\Support\Facades\URL::forceScheme('https');
      

    Update:

    1. Implicitly setting scheme for route group (Laravel5.6):

      Route::group(['scheme' => 'https'], function () {
          // Route::get(...)->name(...);
      });
      
    0 讨论(0)
  • 2020-12-13 04:23

    I used this at the end of the web.php or api.php file and it worked perfectly:

    URL::forceScheme('https');
    
    0 讨论(0)
  • 2020-12-13 04:26
    public function boot()
    {
      if(config('app.debug')!=true) {
        \URL::forceScheme('https');
      }
    }
    

    in app/Providers/AppServiceProvider.php

    0 讨论(0)
提交回复
热议问题