Force Laravel to Use HTTPS Connection for all Links
Hi artisans, in this article I’ll show how to force Laravel project to use HTTPS for all links such as routes, assets. Let’s start:
Table of Contents
Common Config
At first, you have to ensure that you’ve set up SSL & it is working perfectly. Then open the .env file and set the HTTPS link of your website to APP_URL:
APP_URL=https://domain.com
After updating the .env file in the production server, sometimes the updated variable doesn’t show updated content. In this situation, we have to clear cache of our application.
If your application is running on Apache, force HTTPS with .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
For Nginx server:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 301 https://example.com$request_uri;
}
Now you can visit your website URL and check it works or not. If it doesn’t work, then follow the next steps.
Force HTTPS for All Routes
Open app/Providers/AppServiceProvider.php
file and update boot()
method with this:
use Illuminate\Support\Facades\URL;
public function boot()
{
URL::forceScheme('https');
}
If can also set condition like:
// condition 1
if (env('APP_ENV') === 'production') {
URL::forceSchema('https');
}
// condition 2
if (env('APP_ENV') !== 'local') {
URL::forceScheme('https');
}
// condition 3
if (env('APP_FORCE_HTTPS', false)) {
URL::forceScheme('https');
}
Force HTTPS for Pagination Links
If you notice that all routes and assets links are using HTTPS connection but pagination links are using HTTP. In this situation, add this line in the AppServiceProvider‘s boot()
method:
use Illuminate\Support\Facades\URL;
public function boot()
{
if (env('APP_ENV') === 'production') {
$this->app['request']->server->set('HTTPS','on'); // this line
URL::forceSchema('https');
}
}
That’s all. Thanks for reading. ?
Comment
Preview may take a few seconds to load.
Markdown Basics
Below you will find some common used markdown syntax. For a deeper dive in Markdown check out this Cheat Sheet
Bold & Italic
Italics *asterisks*
Bold **double asterisks**
Code
Inline Code
`backtick`Code Block```
Three back ticks and then enter your code blocks here.
```
Headers
# This is a Heading 1
## This is a Heading 2
### This is a Heading 3
Quotes
> type a greater than sign and start typing your quote.
Links
You can add links by adding text inside of [] and the link inside of (), like so:
Lists
To add a numbered list you can simply start with a number and a ., like so:
1. The first item in my list
For an unordered list, you can add a dash -, like so:
- The start of my list
Images
You can add images by selecting the image icon, which will upload and add an image to the editor, or you can manually add the image by adding an exclamation !, followed by the alt text inside of [], and the image URL inside of (), like so:
Dividers
To add a divider you can add three dashes or three asterisks:
--- or ***

Comments (0)