Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mixed Content (laravel)

Tags:

laravel-5

I get the following error (on every page)

app.js:703 Mixed Content: The page at 'https://sitename.com/' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://sitename.com/index.php/getMessages'. This content should also be served over HTTPS.

The site is build with Laravel. Is there anybody who knows how to fix this error?

like image 395
AtomisedClarity Avatar asked Oct 30 '17 15:10

AtomisedClarity


2 Answers

If you are moving the website from HTTP to HTTPS, and it's was working perfectly on HTTP, and if you have added the new URL with https in config/app.php and also in the .env file then you may need to add the below snippet in your app/Providers/AppServiceProvider.php file's boot function and do not forget to add "use Illuminate\Support\Facades\URL;" at the top of the file to fix this error. Please check attached for better sample code

use Illuminate\Support\Facades\URL;

public function boot()
{
       URL::forceScheme('https');
}
like image 169
Vineet Singh Avatar answered Sep 18 '22 17:09

Vineet Singh


In my case it's because I wasn't aware that the asset() function didn't handle https automatically (as pointed out by frankfurt-laravel's answer).

To get around this, since I don't use SSL in dev, I set ASSET_URL in the .env to the https url:

APP_URL=https://example.com
ASSET_URL="${APP_URL}"

This overrides the asset() function to use the https url, without having to modify the function at all. See the docs for more context.

like image 26
shaneparsons Avatar answered Sep 18 '22 17:09

shaneparsons