Technology

Seamless Domain Transfer with Handy Rewrite+PHP Source Code

Learn how to seamlessly transition your website to a new domain with efficient URL redirection using Rewrite and PHP source code.

Sangrak

At times, managing a website involves transitioning to a new domain. This could be due to moving from a less appealing or free domain. A major concern is that URLs disseminated across vast internet forums and search engines become obsolete post-domain transfer. Typically, navigating to the old domain redirects users to just the homepage of the new domain, disregarding the original sub-addresses. But what if redirecting maintained the sub-addresses by simply changing the domain part?

This can be easily achieved with just the .htaccess file and a single PHP file.

Here's the code for the .htaccess file, typically located at /home/redirect/public_html/.htaccess:

RewriteEngine on
RewriteRule ^(.*) /home/redirect/public_html/index.php [L]

And the following is the index.php file, located at /home/redirect/public_html/index.php:

<?
Header("Location: http://yournewdomain.com");

Implementing this strategy is surprisingly straightforward.

More on this topic