Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSL for relative paths?

I noticed when accessing a site using HTTPS I get errors in the JS console when trying to include CSS or JS files from a relative path such as this:

<link rel="stylesheet" type="text/css" href="css/demo.css?id=14" />

Mixed Content: The page at 'https://mysiste.com/' was loaded over HTTPS, but requested an insecure stylesheet 'http://mysiste.com/css/demo.css?id=14'. This request has been blocked; the content must be served over HTTPS.

What's the ideal solution for this scenarios?

  • Should I just force all the HTTP accesses to be redirected to HTTPS?
  • Or is there a way to tell the server to serve all relative paths using HTTPS?
like image 727
Alvaro Avatar asked Sep 16 '26 03:09

Alvaro


1 Answers

Relative links will use the protocol and host from the base URL. The base URL is usually the one which can be seen in the URL bar. But it is possible to change the base URL explicitly using the base tag.

This means if you have a base URL with http as protocol set in your page it will still use http instead of https even though you've accessed the site with https and the reference is relative, i.e. like this:

<base href="http://example.com/">

To fix it either remove the base tag or change it to use https instead of http:

<base href="https://example.com">
like image 112
Steffen Ullrich Avatar answered Sep 19 '26 06:09

Steffen Ullrich