Tuesday, 14 June 2016

Wordpress HTTPS site setup behind an SSL terminating Loadbalancer

I've recently been carrying out work on Wordpress multi-site functionality for some websites. More recently, configuring Wordpress sites over HTTPS. There is a lot of tutorials out there on how to set it up, but many of them assume you are not using any kind of load balancing technology or SSL termination. After hours of troubleshooting why my new multi-site was not displaying any data at all over HTTPS (after changing the site URLs and adding in my relevant entries in .htaccess as instructed), I found that you need to modify the wp-config.php to turn on HTTPS if the X-Forwarded-Proto header passed from the load balancer contains 'https'. This is the snippet of code you need to have in your wp-config.php:

if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
$_SERVER['HTTPS'] = 'on';

This needs to go directly above the "/* That's all, stop editing! Happy blogging. */" line.

This will only work for Loadbalancers which support passing the X-Forwarded-Proto header, in this case, I am using Amazon's ELB (Elastic Loadbalancer)

Additionally, ensure your .htaccess file contains the relevant rewrite rules for your site domain, e.g:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
</IfModule>

If you wish to be selective on what domain you want to rewrite - add the additional conditional statement line:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
</IfModule>

Platform Engineering: Developer Experience

Intro The role of Platform Engineer as evolved through the ages. I’ve gone through all the names: Sys Admin, Infrastructure Engineer, DevOps...