Apache – Redirect Site To Another URL

Redirect Site to Another Domain

Let’s say you want to redirect your website (e.g www.example1.com) to another domain (e.g www.example2.com), then add the following VirtualHost tags to your server configuration file.

<VirtualHost *:80>
ServerName www.example1.com
Redirect 301 / http://www.example2.com/
</VirtualHost>

<VirtualHost *:80>
ServerName www.example2.com
. . .
. . .
</VirtualHost>

In the above code, we create 2 Virtual Hosts, one for each domain. We configure virtual host for example1.com to redirect all requests to example2.com (in bold)

OR VIA HTACCESS:

Using Redirect

RewriteEngine on
Redirect 301 / http://example2.com/

 

Using RewriteRule

RewriteEngine on
RewriteCond "%{HTTP_HOST}"   "!^www\.example1\.com" [NC]
RewriteCond "%{HTTP_HOST}"   "!^$"
RewriteRule "^/?(.*)"        "http://www.example2.com/$1" [L,R,NE]