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

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.

Was this answer helpful? 0 Users Found This Useful (0 Votes)