In the last post about my WordPress server configurations, I had the static pages working, but I couldn’t get dynamic pages working, and I had to use the EC2 public domain directly instead of the one configured in Route53 to use the dynamic pages.
The main issue was that the CSS for the admin wouldn’t load due to http and https incompatibilities between CloudFront and EC2. I tried a bunch of things like configuring wp-admin.php but WordPress isn’t fully loaded when those get ran.
First, fix HTTP to HTTPS in the database:
UPDATE wp_options SET option_value = 'https://www.reikaxubia.com' WHERE option_name = 'home';
UPDATE wp_options SET option_value = 'https://www.reikaxubia.com' WHERE option_name = 'siteurl';
Next, configure wp-config.php for CloudFront detection:
// Detect CloudFront and force HTTPS context
if (isset($_SERVER['HTTP_VIA']) && strpos($_SERVER['HTTP_VIA'], 'cloudfront') !== false) {
$_SERVER['HTTPS'] = 'on';
$_SERVER['SERVER_PORT'] = 443;
$_SERVER['REQUEST_SCHEME'] = 'https';
}
// Basic WordPress constants
define('WP_HOME', 'https://www.reikaxubia.com');
define('WP_SITEURL', 'https://www.reikaxubia.com');
define('FORCE_SSL_ADMIN', true);
Next, I had to add the following lines to the end of the file: /var/www/html/wp-content/themes/twentytwentyfour/functions.php
// Force HTTPS for admin assets
add_filter('script_loader_src', function($src) {
return str_replace('http://', 'https://', $src);
});
add_filter('style_loader_src', function($src) {
return str_replace('http://', 'https://', $src);
});
Lastly make sure that CloudFront caching policy to CachingDisabled.
Make sure you get rid of your cache, or log in to your WordPress in incognito mode. And that’s it!
Getting WordPress working fully has been a long journey, and there’s files on the EC2 server that I wasn’t expecting to touch to do so. I knew theoretically I should have been able to go from Route53 DNS to CloudFront to EC2 WordPress server.
If it can theoretically work, you can probably get it working. Sometimes you need to go places that you weren’t planning to go to get it working, and it’s up to you to decide if it’s worth it.
Leave a Reply