Force HTTPS Redirects in Laravel
Redirect all HTTP traffic to HTTPS in a Laravel application using .htaccess (Apache) or the AppServiceProvider.
Option 1 - .htaccess (Apache)
Add to public/.htaccess:
Options -MultiViews
RewriteEngine On
# Redirect HTTP to HTTPS
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
# Remove trailing slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Laravel front controller
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]Option 2 - AppServiceProvider
In app/Providers/AppServiceProvider.php:
use Illuminate\Support\Facades\URL;
public function boot()
{
if (config('app.env') === 'production') {
URL::forceScheme('https');
}
}Verify
curl -I http://example.comExpect a 301 redirect to https://example.com.