Setup redirection from one domain to another domain on .htaccess

Date Posted: 24-08-2017


There are scenarios like where we want to redirect onedomain to another domain. We can use .htaccess to achieve the same.


  1. Ubuntu or centos Server
  2. Apache 2.4 or apache 2.2
  3. Rewrite module. You can verify whether rewrite module installed or not using the below command

apachectl -M


Get the documentroot of a domain. If its a cPanel server, it will be /home/username/public_html.

If its a ubuntu plain server, documentroot will be /var/www/html

You can alternatively, use the below command to find the documentroot.

apachectl -S

Open .htaccess file and append the following line.

vi .htaccess

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule (.*)$1 [R=301,L]

Where, => Old domain name => New Domain name

Access the domain on the browser, it will automatically redirects to


Leave a Reply