Splitting Bound Domains – Keeping Everyone Happy

Having two domains mirroring one another, I wanted to split them up and actually use them for different purposes. But how can one safely split apart the domains without upsetting the folks and search engines referencing the domain that is going to be used for a new purpose? It turns out rather easily!

Just to make things easier to understand, I will share that www.eclecticthoughts.com and www.kieransmith.net pointed to the same site, and explain that I wanted to use www.kieransmith.net for a new site – a single page listing all my ‘homes’ on the ‘net. I knew that search engines and friends (and probably some other folks) were referencing content on the www.kieransmith.net domain, so if I were to simply remove the mirroring, they would end up getting 404 pages if they tried to access the pages there.

The first thing to do is make sure that the new site has a custom 404 page; the browser should show a page, and not an error when someone hits a page that doesn’t exist. If you don’t have this set up, it is rather easy to do. I use PHP as my programming language of choice, so modify these to suite your needs if you use a different language. To have the browser show a custom 404 page, simply add the following line to either your virtual-host entry or a .htaccess file in your document root:

ErrorDocument 404 /404.php

Make sure you create your 404 file, too. It won’t do anyone any good if you point folks who can’t find a page to another page that doesn’t exist! By using PHP for our 404 page, we’re able to put some smarts behind the page, and maybe even serve up the “lost” page. Using a mix of cURL and headers in PHP, one can check if a page exists somewhere else, and if it does, redirect the visitor to the now-not-lost page. Here’s the script I came up with:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	$ch = curl_init( "http://www.eclecticthoughts.com" . str_replace( " ", "+", $_SERVER["REQUEST_URI"] ) );
 
	curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
 
	curl_exec\( $ch );
 
	$info = curl_getinfo( $ch );
 
	curl_close($ch);
 
	if( $info["http_code"] == "200" )
	{
		// If we got a 200 status code (versus a 404), the page was found.
		// We can now throw a 301 (Moved Permanently) and redirect to the new address.
		header( "Location: http://www.eclecticthoughts.com" . str_replace( " ", "+", $_SERVER["REQUEST_URI"] ), TRUE, 301 );
	}
	else
	{
		// Put your 'Oops, we lost your page content here.
	}

Note: The line “curl_exec\( $ch );” should not have the slash in it. Adding this slash was the only way I could sneak this line of code into the post. Apparently all the other code goes through just fine, but this one borked WordPress when I posted it.

It is that easy! Of course, you need the curl PHP extension set up, and if you don’t have access to your virtual-host file, you’ll need your hosting provider to allow .htaccess files, but most should have both of these conveniently set up, so the code provided should work ‘out of the box.’

Let me know if you have any additional ideas or run into any problems. I’d love to hear from you!

, ,

Leave a reply