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 Even if you type (without www) in the address bar, you will be automatically redirected to This behaviour was broken. When I typed, I was taken to in the browser. But when I typed, browser was not opening Instead of that, it was taking me to 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 (without www), the field value was And when I access admin panel as, the value was 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