How to redirect all 404 requests to another domain

This article explains the details of the settings to redirect all 404 requests to another domain.

Step1: Open the .htaccess file.

#vi .htaccess

Step2: Update the below lines to the file.

ErrorDocument 404 /404.php

Step2: Create a file 404.php.

#vi 404.php

Step3: Save the following lines to this file.

<?php
if ($_SERVER["HTTP_HOST"] == "domain1.com") // current domain
header('Location: http://domain2.com' , TRUE, 301);
?>

Note to replace domain1.com and domain2.com appropriately.

Leave a Reply

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