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

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}', ['uses' => ' OrderController@details ', 'as' => 'order.details', 'https']); url($language.'/index', [], true) asset('css/bootstrap.min.css', true) 

But setting parameters all the time seems tedious.

Is there a way to get all routes to generate HTTPS links?

+21
source share
8 answers

You can set 'url' => 'https://youDomain.com' to config/app.php , or you can use the Laravel 5 middleware class - redirect to HTTPS .

+18
source

Here are some ways. Choose the most convenient.

  1. Configure your web server to redirect all insecure requests to https. Example nginx configuration:

     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 the environment variable APP_URL using https:

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

  4. Add the following line to the AppServiceProvider :: boot () method (for version 5. 4+):

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

Update:

  1. Implicit scheme setup for a route group (Laravel5.6):

     Route::group(['scheme' => 'https'], function () { // Route::get(...)->name(...); }); 
+14
source

Put this in the AppServiceProvider in the boot () method

 if($this->app->environment('production')) { \URL::forceScheme('https'); } 
+12
source

Add this to your .htaccess code

 RewriteEngine On RewriteCond %{SERVER_PORT} 80 RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L] 

Replace www.yourdomain.com with your domain name. This will force all your domain URLs to use https. Verify that the https certificate is installed and configured on your domain. If you don't see https green as safe, press f12 on chrome and fix all the mixed errors in the console tab.

Hope this helps!

+6
source

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

 URL::forceScheme('https'); 
+5
source

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] 
+4
source
 public function boot() { if(config('app.debug')!=true) { \URL::forceScheme('https'); } } 

in the application / Providers /AppServiceProvider.php

0
source

try this - it will work in the RouteServiceProvider file

  $url = \Request::url(); $check = strstr($url,"http://"); if($check) { $newUrl = str_replace("http","https",$url); header("Location:".$newUrl); } 
-1
source

All Articles