non-www to www Redirection Problem in WordPress Installed by Bitnami

Recently I moved this WordPress blog to Google Cloud. I used WordPress by Bitnami one click installation to setup a new WordPress blog. And then I migrated existing blog data to the new one. I just had to change DNS A record to point to the new IP address. And WordPress migration happened without any downtime. I was doing few checks after that to make sure things were working as expected. Overall things were fine except a few minor glitch. But I found one issue regarding non www to www redirection. You can see that this website can be accessed only as www.w3spot.com. Even if you type w3spot.com (without www) in the address bar, you will be automatically redirected to www.w3spot.com. This behaviour was broken. When I typed www.w3spot.com, I was taken to https://www.w3spot.com in the browser. But when I typed w3spot.com, browser was not opening www.w3spot.com. Instead of that, it was taking me to https://w3spot.com. No redirection was happening.

So how did I solve this problem? I knew it was not caused by any recent DNS change. Only thing I changed was IP address in A record in DNS settings. That can’t cause this issue. So I checked WordPress settings page. I noticed that WordPress Address (URL) & Site Address (URL) fields were greyed out. But when I access admin panel as w3spot.com (without www), the field value was http://w3spot.com. And when I access admin panel as www.w3spot.com, the value was http://www.w3spot.com. So the value was getting changed according to the website address used in the browser. With some searching, I found this code in wp-config.php:

define('WP_SITEURL', 'http://' . $_SERVER['HTTP_HOST'] . '/');
define('WP_HOME', 'http://' . $_SERVER['HTTP_HOST'] . '/');

Based on the host value provided by browser client, WordPress Address & Site Address urls were getting changed. And that broke non-www to www domain redirection. For now, I have just edited & hardcoded the value in wp-config.php. Root to www redirection is working fine.

Leave a Comment