domain forwarding with Apache mod_rewrite

You might have the domain domain1.com which should redirect visitors to domain2.com. You could set up a virtual host, pointing to /path where an index.php file resides which forwards the user, e.g. with:

<?php
header("Location: http://domain2.com");
?>

The easier way is to create a virtual host, e.g. by creating /etc/apache2/sites-available/domainForwarding with the following content:

<VirtualHost *:80>
ServerName domain1.com
ServerAlias w.domain1.com
ServerAlias ww.domain1.com
ServerAlias www.domain1.com
ServerAlias wwww.domain1.com
ServerAdmin admin@domain1.com
#may be you want some logging: (uncomment he next two lines)
#ErrorLog        /var/www/domainForwarding/logs/error.log
#CustomLog       /var/www/domainForwarding/logs/access.log common
Options -Indexes
RewriteEngine     On
RewriteRule ^.*$ http://www.domain2.com [R,L]
</VirtualHost>

This makes mod_rewrite (which must be enabled) to forward the request whithout the need for additional files.

Note: run a2ensite domainForwarding to enable the site and /etc/init.d/apache2 reload to update the apache’s configuration.

Leave a comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.