Redirect Non-www to www or www to Non-www

Page content

How to Redirect from non-www to www or vice versa

Most people now use non-www versions of urls for their blogs and websites. While it keeps the url shorter, there is no need to always add the www to the url when typing. If you want to use the non-www versions of urls as the canonical urls on your blog or website, you will need to redirect the www version of your website to the non-www version. Google counts www and non www as different and so rather than keeping both, you should use one and set it as the canonical version.

Redirecting the www version to non www version and setting the former as canonical will help you avoid duplicate content issues. If you have both the www and non www versions enabled and you have not set the canonical version, you will most likely run into duplicate content issues. Therefore, you must 301 redirect www versions to non www versions or vice versa. The key is to keep only one version since keeping both will result in duplicate content related issues.

Enable mod_rewrite

However, before you can create redirects on your apache server, you need to make sure that mod_rewrite is enabled. Check if the module is enabled on your server with the following command:

sudo apachectl -M

It will output a list of all the loaded modules. If the rewrite module is not included in the list, you can enable the module with a simple command.

Sudo a2enmod rewrite

Following that, you must restart the Apache server.

Sudo systemctl restart apache2

Now that you have the rewrite module enabled, you can complete the rest of the steps to redirect www to non www versions or vice versa.

Enable .htaccess file

You will need to edit the .htaccess file to create the redirect. Check and make sure that you have the .htaccess file enabled on your Apache server. If it is not, you will first need to enable it on your server. To enable the .htaccess file for a website, you will need to edit its Virtual hosts file and add the following:

<Directory /var/www/html>

AllowOverride All
</Directory>

Add it inside the virtual hosts file. Your virtual hosts file can be found inside the sites-available directory.

Sudo nano /etc/apache2/sites-available/example.conf

Now, edit the Vhosts file of your website so that the file looks like the following:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /var/www/html>
AllowOverride All
</Directory>

</VirtualHost>

Replace /var/www/html with the root folder of your website and you are done. Now save the file and exit. (Ctrl + S) (Ctrl + X)

Now, you need to restart the apache server so that the changes can be applied.

Sudo systemctl restart apache2

Now, you can add a .htaccess file to your root folder.

Sudo nano /var/www/html/.htaccess

Redirect www to non-www version

Now, add the following content inside the .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]

Replace yourdomain with your real domain name. This will redirect all the www urls on your website to the non www versions. For example www.example.com/some-page/ will be redirected to example.com/some-page/ and so on.

Redirect non www to www version

Some people prefer to keep the www versions of the url. No problem. In that case too, you must redirect the non www urls to the www urls so that you do not face any duplicate content issues. You will need to add the following code inside the .htaccess file to redirect all the non www urls to the www urls.

RewriteEngine On
            RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
            RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Or, you can use the following redirect rule:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Both the above rules redirect the non www version to the www version.