How to Redirect Your Website from www to Non-www

Hits: 37

Nowadays, it is redundant and unwieldy to include www in your website address, as most websites are going without www in their URL, also known as the naked domain or non-www. It just looks and sounds more concise and clean to just remove the www entirely from the website address. I build dynamic websites on Bitnami WordPress Stacks, powered by AWS on EC2 instances, and powered by GCP (Google Cloud Platform) on Compute Engine instances. I decided earlier this year to transfer my websites away from traditional web hosts, as it was costing me $10 to $15 a month for each website…and when you have dozens of websites, this adds up.

Given my rising costs of website hosting fees, and my ever-improving tech skills, I decided to look into AWS and GCP to look at the feasibility of actually transferring and maintaining websites on my own, on my own rented server space in the cloud. As it turns out, the costs for hosting websites on AWS and GCP are literally just pennies on the dollar, as most of the services in the cloud for AWS are free tier (until you go over the generous limits they set), and GCP gives you a 1 year free trial with $300 credit for developers. But if you don’t have tech experience, then hosting your own website on the cloud can be difficult- but if you have multiple websites and want to learn, then there are many tutorials out there to guide you on the process of building and hosting your own website on the cloud. If you can’t be bothered, then stick with web-hosting companies, as $10 to $15 per month is not bad, considering you are paying them to help you build and maintain your site with minimal tech skills.

So with WordPress sites, there are a few plugins to help with the process of redirecting your website address from www to non-www. However, I tried to install and activate the redirection plugin All In One Redirection, but all it did was break my websites, as it threw it into an infinite recurring redirection. That’s when I decided to just SSH into the web files on the server, and change the configuration files myself.

The first step is to SSH into your webfiles. For GCP, you can SSH directly from the SSH button on the dashboard of your Compute Engine instance. For AWS, you can SSH with PuTTY. Once you are connected to the server via SSH, connect as a root user:

 
 
  1. sudo -i

and open the wp-config file in the nano editor:

 
 
  1. nano /opt/bitnami/apps/wordpress/htdocs/wp-config.php

The wp-config file will open in the nano editor, and then you will find these 2-lines of code in the file:

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

Replace the above two lines of code with the following:

 
 
  1. define('WP_HOME','https://example.com');
  2. define('WP_SITEURL','https://example.com');

Replace example.com with your domain. The above example is for redirecting www to non-www. If you want to redirect non-www to www, then use the following code:

 
 
  1. define('WP_HOME', 'https://www.example.com');
  2. define('WP_SITEURL', 'https://www.example.com');

Now press CTRL+O to save the changes to the file, and CTRL+X to exit back to the command line. As a final step, you need to restart the Apache server by typing the following into the command line:

 
 
  1. sudo /opt/bitnami/ctlscript.sh restart apache

Now test to see if your redirection works by typing both www and non-www domains into the web browser. Voila, done!