0

I have a Wordpress website running on an AWS EC2 instance. This is served through an AWS Elastic Load Balancer, which has HTTPS enabled with a certificate I got from Amazon.

The intention is to serve both an http and an https version of the website. Loading the http version works fine.

When I load the https version however, I'm getting mixed content errors because get_template_directory_uri() always returns http links. The way the load balancer works is the TLS terminates at the LB, and it communicates with the actual EC2 instance over port 80. therefore, there is no HTTPS on the instance itself.

A lot of this is beyond my skill to heal. I know just enough to have figured out what the problem seems to be, but I'm really not sure what the right way to fix it is.

Assuming I still want to serve both http and https versions of the page (there is no ecommerce or auth on the page -- it's just informational), how should I go about fixing this?

FYI, the EC2 instance is running on an Amazon ABI, which is basically RHEL.

John Dibling
  • 99,718
  • 31
  • 186
  • 324

1 Answers1

2

So first off, you will find it difficult to run both an http and https WordPress version off the same database data because WordPress saves a lot of links as absolute links (i.e. with the http(s)://mydomain.com part) and a lot of plugins just don't bother adapting to the current protocol either.

Your best bet is going to be doing redirects through your htaccess file to redirect all http traffic to https.

That being said, one way you could do what you asked for is through a filter used by get_template_directory_uri:

add_filter('template_directory_uri', 'smart_template_directory_uri', 10, 3);
function smart_template_directory_uri($template_dir_uri, $template, $theme_root_uri) {
    return preg_replace('/^https?\:/i', '//', $template_dir_uri); // replace "http://" or "https://" by "//", which browsers will automatically set to the current page's protocol
}

Hope this helps!

nibnut
  • 3,072
  • 2
  • 15
  • 17
  • Thanks. It look slike the code you've posted turns all links in to `https` links. Is that right? So the page itself would be served over `http`, but everything embedded within it would be served over `https`? – John Dibling Jan 10 '17 at 21:37
  • Oh, OK, I get it now. Is it safe to assume that all (let's assume modern) browsers would convert `//` to `http[s]://`? – John Dibling Jan 10 '17 at 21:40
  • Yes indeed - see this answer here: http://stackoverflow.com/questions/4831741/can-i-change-all-my-http-links-to-just – nibnut Jan 10 '17 at 21:41
  • Great link, thanks. I guess the question I really need to ask myself is to I want to support http at all? And then how would I go about serving only https. I dont have my own (very expensive) certificate to put on the EC2 instance. This leads me down a road where there is so much I just don't know. – John Dibling Jan 10 '17 at 21:45
  • Yeah, my choice would be to force everything to be https. A lot of sites are going down that route. As for certificates, there are fee ones out there (letsencrypt.org) that I think you could use with AWS: https://www.google.ca/search?q=lets+encrypt+aws&oq=lets+encrypt+aws – nibnut Jan 10 '17 at 22:50