-1

I have a Amazon ec2 instance with apache webserver installed. I have used Certbot to install a SSl certificate. I want to display a Image on my site using javascript. When a user enters in a edit text box I want to dynamically change a image. Im currently using javascript to change it. When I access the image I get this message "requested an insecure image 'http://myurl.jpg'. This content should also be served over HTTPS. How do I use Https so serve the image?

document.getElementById("img").src="http://myurl.jpg"
amanda45
  • 535
  • 10
  • 29

3 Answers3

0

Replace this: "http://myurl.jpg" with this: "https://myurl.jpg"

easy enough?

John Doe
  • 399
  • 1
  • 4
  • 23
0

Leave out the protocol in the URL. Then it will default to the same protocol as was used for the page. So if the page was loaded with https, it will also use https for the image, and there won't be an error:

document.getElementById("img").src="//myurl.jpg"
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

If you want for example image filepaths to load as HTTPS while still using relative paths you must set a <base href="URL"/>.

I use file paths like this <img src="img/xxx.png" alt="xxx"> but without setting a base URL it will load by default as HTTP. To resolve this add something like this in your head.

<head> <?php $BaseDir = 'https://' . $_SERVER['SERVER_NAME'] .substr($_SERVER["SCRIPT_NAME"], 0, strrpos($_SERVER["SCRIPT_NAME"], "/") + 1); echo '<base href="' . $BaseDir . '" />'; ?> </head>