Setup redirection from one domain to another domain on .htaccess

Date Posted: 24-08-2017

Scenario:

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

Prerequisites:

  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

Solution:

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 (.*) http://www.newdomain.com/$1 [R=301,L]

Where,

domain.com => Old domain name

newdomain.com => New Domain name

Access the domain http://www.domain.com on the browser, it will automatically redirects to newdomain.com

 

Leave a Reply