Load Blade assets with https in Laravel

Load Blade assets with https in Laravel

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Load Blade assets with https in Laravel

When you deploy a Laravel application behind an SSL termination layer, such as Nginx or Apache, and force traffic to HTTPS, browsers enforce strict security policies regarding mixed content. If your application serves HTML over a secure connection but attempts to load external resources like CSS or JavaScript files over HTTP, the browser will block those requests. This results in the infamous "page was loaded over HTTPS, but requested an insecure stylesheet" error you are seeing on your login page.

The root cause lies in how the {{ asset() }} helper function works by default. In Laravel, this helper generates a URL relative to your application's base path unless explicitly told otherwise. However, the generated URL does not automatically inherit the protocol of the current request. When you write <link href="{{ asset('assets/mdi/css/materialdesignicons.min.css') }}">, Laravel typically resolves this to an absolute URL based on your configuration, often defaulting to http:// if no specific protocol is forced or if the environment variable isn't set correctly for the build process.

To resolve this without relying on global environment variables which might cause issues during local development, you need to explicitly instruct Blade to use the protocol of the incoming request. Laravel provides a specific blade syntax for this exact scenario: {{ asset('path', ['secure' => true]) }}. By passing an array with the secure key set to true, you tell the framework to prepend https:// if the request is secure, or fall back to http:// if it isn't. This ensures your assets always match the security context of the current connection.

Here is how you should update your Blade template:

<link 
    href="{{ asset('assets/mdi/css/materialdesignicons.min.css', ['secure' => true]) }}" 
    media="all" 
    rel="stylesheet" 
    type="text/css" 
/>

This approach is robust because it handles the transition between HTTP and HTTPS dynamically. If you are testing locally without SSL, the link will still work over http://. Once you deploy to production with an HSTS-enabled load balancer or a reverse proxy, the same code will automatically serve the CSS over https://, eliminating mixed content warnings.

It is important to understand that this is indeed Blade's job within the Laravel ecosystem. While you could manually construct URLs using config('app.url') and string manipulation, relying on the built-in helpers keeps your codebase cleaner and less prone to protocol mismatch errors. The asset() helper with the secure flag is the recommended pattern for modern Laravel versions.

There are other scenarios where this logic applies beyond just CSS links. You should apply the same ['secure' => true] parameter to any <script> tags, <img> tags, or iframes loading resources from your public directory. For example:

<script src="{{ asset('js/app.js', ['secure' => true]) }}"></script>
<img src="{{ asset('images/logo.png', ['secure' => true]) }}" alt="Logo">

A common pitfall occurs when developers mix relative paths with absolute URLs in their Blade templates. If you use a hardcoded URL like https://example.com/assets/..., you lose the flexibility of the application's domain configuration, and if that domain changes (e.g., from staging to production), you must manually update every single asset tag. Using asset() ensures your application remains agnostic to the specific domain name while respecting the security protocol required by the browser.

If you are using Laravel Mix or Vite for asset compilation, ensure that your build pipeline respects these directives. The compilation process minifies and bundles your assets, but it does not alter the URL generation logic of the Blade templates themselves. As long as your view files use the correct asset() syntax with the secure flag, the compiled bundle will be loaded correctly regardless of the underlying protocol.

For more advanced asset management strategies or if you are integrating third-party packages that might bypass standard helpers, always verify their documentation to see if they support the secure option or require specific configuration in your config/app.php. Consistency in how you load resources is key to maintaining a secure user experience and preventing potential security vulnerabilities associated with mixed content.

Our network