Redirect domain.com to www.domain.com and vice versa

Posted at April 4, 2016 at 3:23 pm by Jithin

Redirect domain.com to www.domain.com    

In this documentation, we can check how to redirect domain.com to www.domain.com and redirect www.domain.com to domain.com. We can do this by adding the rewrite rule in the .htaccess file. ( It is also possible to do the same by using cPanel Redirect tool. More details of that method is available here )

 

We can use the following steps to do this.

 

1) Go to the home directory.

cd /home/username/public_html

(Replace the username with the exact username).

If you are not using cPanel server, go to your home directory.

 

 

2) Open the .htaccess file.

vi .htaccess

Or use your favorite text editor like nano

3) Add the following lines in .htaccess file and save the file.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain.com [NC]

RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

(Replace domain.com with your exact domain name).

 

 

There is no need to restart apache.

From now onward, when someone access your domain.com will redirect to www.domain.com.

 

Same as we can redirect www.domain.com to domain.com using the following steps.

 

1) Go to the home directory.

cd /home/username/public_html

(Replace the username with the exact username).

If you are not using cPanel server, go to your home directory.

 

 

3) Open the .htaccess file.

vi .htaccess

 

4) Add the following lines in .htaccess file and save the file.

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.domain.com [NC]

RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

(Replace domain.com with your exact domain name).

 

 

There is no need to restart apache.

From now onward when someone access your www.domain.com will redirect to domain.com.

 

That is how we can redirect domain.com to www.domain.com and redirect www.domain.com to domain.com.

 

If you need any further help please reach our support department.

 

 

0.00 avg. rating (0% score) - 0 votes

You can skip to the end and leave a response. Pinging is currently not allowed.

2 Responses to “Redirect domain.com to www.domain.com and vice versa”

  1. Fedi says:

    thanks for this tutorial but my domain is linked to blogger. what can i do ?

Leave a Reply