How to force Laravel Project to use HTTPS for all routes?
Manually appending https to every single route definition, URL helper call, and asset link is inefficient and prone to oversight. While passing a third argument like [true] to the asset or url helpers works as intended, relying on it for every instance creates unnecessary boilerplate. It forces developers to remember to toggle a flag whenever they need a secure link, even when the underlying application logic remains identical.
The fundamental issue is that Laravel's URL generation defaults to HTTP if no scheme is explicitly provided. The framework does not inherently know your environment requires HTTPS at the application level without configuration. To remove the friction of passing true repeatedly, you must define the application scheme globally before any route matching occurs.
Configure the Application Scheme
The most robust approach is to set the default scheme in your .env file or via a service provider. By defining the APP_URL environment variable, you instruct Laravel on what the base URL of your application should be. If this value includes https://, all generated URLs will respect that scheme unless explicitly overridden.
In your .env file:
APP_URL=https://your-domain.com
This single line effectively forces all standard URL helpers to generate HTTPS links. However, this only applies to relative paths and standard URL generation. If you are dealing with absolute paths or specific asset loading scenarios, additional configuration might be necessary depending on your deployment setup.
Handling Mixed Content in Production
Even if you configure APP_URL correctly, browsers will often flag your application as "Mixed Content" if the page is served over HTTPS but attempts to load assets (like images or CSS files) over HTTP. This usually happens when you are using a local development server (like PHP built-in server or Homestead) that defaults to HTTP, while trying to access it via an HTTPS proxy or simply because the environment variable isn't set correctly for local testing.
To ensure consistency, you should also consider configuring your server block in Nginx or Apache to redirect all HTTP traffic to HTTPS automatically. This is a server-level enforcement that bypasses Laravel's application logic entirely. For example, an Nginx configuration can listen on port 80 and issue a 301 redirect to the same path on port 443.
The Limitation of Route-Specific Flags
It is important to understand why the third parameter in your route definitions (['https']) is less desirable as a primary strategy. While valid, it couples the scheme requirement directly to the route registration logic rather than the environment. This creates a divergence between development and production environments if not handled carefully. In a local setup where APP_URL defaults to http://, simply adding ['https'] to a route definition might still result in mixed content issues if the asset helper isn't also told to use HTTPS.
For instance, consider this scenario:
Route::get('order/details/{id}', ['uses' => 'OrderController@details', 'as' => 'order.details', 'https']);
$url = url($language.'/index', [], true); // This uses the second parameter for scheme
$css = asset('css/bootstrap.min.css', true); // This also requires the second parameter
If APP_URL is set to HTTPS, you technically don't need to pass true to the url or asset helpers. However, if you rely solely on route-level flags without setting APP_URL, you are forced to maintain that flag in every controller action and closure that registers a route. This increases cognitive load during development and refactoring.
Conclusion
The cleanest solution is to establish the scheme at the configuration level. By ensuring your .env file contains APP_URL=https://..., you eliminate the need for repetitive flags in your code. This approach aligns with Laravel's philosophy of convention over configuration, allowing your application to adapt automatically to the environment it is running in. If you are using a specific hosting provider or custom deployment pipeline, verify their documentation on how they handle APP_URL propagation to ensure the setting takes effect across all facets of your stack.
