Apache - How to handle URL redirect based on a requested domain using .htaccess file?

This is how you handle the URL redirection based on a requested domain in Apache by using .htaccess file. Requirements: Apache Module: mod_rewrite installed and enabled. How to: Open .htaccess...

This is how you handle the URL redirection based on a requested domain in Apache by using .htaccess file.

**Requirements: **Apache Module: mod_rewrite installed and enabled.

How to:

Open .htaccess file and add in one of the following codes.

This is what you do if you just want to simply point one domain (abc.com) to another (xyz.com):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc\.com [NC]
RewriteRule ^(.*)$ http://xyz.com/$1 [R=301,L]

And this is how you handle a redirect of a domain (abc.com) to a specific page on a different domain (xyz.com/page.html):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc.com
Rewriterule ^(.*)$ http://xyz.com/page.html [L]