Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MrMoto9000's avatar

Why is asset() not secure?

According to Laravel's documentation: "The asset function generates a URL for an asset using the current scheme of the request (HTTP or HTTPS)"

https://laravel.com/docs/master/helpers#method-asset

I have a website hosted at https:// but Laravel is still generating http:// when I used asset(). I don't want to have to force HTTPS through forceScheme or anything, as I've never had to do this before with Laravel and https:// connections. The documentation states it will use the "current scheme".

0 likes
5 replies
JohnBraun's avatar

Thats strange, as I cant reproduce your problem. When using asset() on https I also see that the asset() url is prefixed with https. Did you provide the https prefix in the APP_URL parameter (in .env)?

Are you using Lets encrypt?

I see you could also use the secure_asset() function from the Laravel docs: https://laravel.com/docs/master/helpers#method-secure-asset

MrMoto9000's avatar

Thanks. It's really odd. I'm deploying to AWS, but I can see my environment variables, and it's as I expect:

APP_URL: https://www.example.com ("example" inserted for privacy)

I've looked through the Laravel source and it's indeed looking at that variable...

if (! function_exists('asset')) {
    /**
     * Generate an asset path for the application.
     *
     * @param  string  $path
     * @param  bool|null  $secure
     * @return string
     */
    function asset($path, $secure = null)
    {
        return app('url')->asset($path, $secure);
    }
}

So why is it changing it to http://www.example.com? So strange! :(

My local .env file is http://localhost, but that's always what I have it set to. None of those local environment variables are being used anywhere, they're all being replaced by the AWS config.

JohnBraun's avatar

Aha!

I don't have experience with load balancers, but I am glad that you've solved it 👍

Please or to participate in this conversation.