Changing Domains with a 301 Redirect
When I moved from my old “cssdev.com” domain to “blazenewmedia.com” I didn’t want to lose my search engine rankings, indexed pages, or bust old bookmarks. Here’s how I did it.

Firstly, my biggest problem was I used Wordpress for my old blog. This site runs on Textpattern (for now), so all my old post archive links from Wordpress don’t match the same format as the their Textpattern alternatives.
Here’s an example. When someone visits an article from my old site, they might hit up the following address:
http://www.cssdev.com/archives/2006/03/19/css-tweak/
Now, in Textpattern this same article can be found at a much simpler link:
http://www.blazenewmedia.com/articles/css-tweak/
So, how can I make my old Wordpress installation forward people to a new Textpattern formatted link, while still saving my search engine rankings, and ensuring the new page gets re-indexed in the same position?
301 Redirects to the Rescue
By sending a “301: Moved Permanently” header to the browser, it’s possible to tell anyone who visits my old Wordpress installation that the content has moved for good.
Not only that, search engine spiders will follow this redirect and re-index all of my pages in place of the old ones. This preserves search engine rankings and prevents people from seeing a whole bunch of dead links when they find my old site on Google.
To get this to work with Wordpress, all I had to do was place a few lines of PHP code into the “index.php’ file in the root of the site. This file is the hub that all Wordpress pages run through.
Here’s the PHP code I used to send a 301 redirect:
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.blazenewmedia.com/blog/");
die;
That solved redirecting, but that didn’t solve my problem with incompatible URLs between Wordpress and Textpattern.
To fix this problem, I needed to grab the page that the user had requested, reformat it so that it matched Textpattern’s formatting, then forward them to the new link.
This was done with the following code:
/**
* Get the requested page: eg /archives/2006/03/19/css-tweak/
*/
$requestedPage = $_SERVER['REQUEST_URI'];
/**
* Split the requested page
* into and array of sections
* using the "/" as a splitter.
*/
$reqArray = explode("/", $requestedPage);
/**
* Check to see if the second, third
* and fourth sections (starts at 0)
* are numbers, if they are we can
* assume these are dates.
*/
if( is_numeric($reqArray[1]) &&
is_numeric($reqArray[2]) &&
is_numeric($reqArray[3]) ) {
/**
* Send the correct 301 header.
*/
header("HTTP/1.1 301 Moved Permanently");
/**
* Send them to the new textpattern
* formatted URL. We know that the last
* section of the URL is the same (/css-tweak/)
* so we can append that to the end of the URL.
*/
header("Location: http://www.blazenewmedia.com/articles/" . $reqArray[4]);
die; // stop anything else from executing.
}
I’ve had the new site up for just over a week now. Google has already almost completely re-indexed my new site. The links to my old CssDev site are reducing by the day. Soon, all the old CssDev links will be completely converted over to Blaze. It works!
If you’re looking to change your domain, or move your site, 301’s are definitely the way to go. With a little PHP knowledge you can also handle redirecting old bookmarked links, even if the formatting has changed. For me, 301’s have worked flawlessly.
8 People Added Their Comments
Good job. I was trying to do that earlier but the the redirect wasn’t… well, redirecting. I’ll file this away for future reference. I’m sure this’ll come in handy.
Question:
I’m not that familiar with php but I’m trying.
I moved my WP blog from here jeff.scofer.com/thinkingstick
to
http://www.thethinkingstick.com
I’ve put in the 301 script that you gave and it now redirects to my new domain, but if I type the direct path to an article such as http://jeff.scofer.com/thinkingstick/?p=10
it is redirected to the home directory of the new domain.
They are both WP blogs and they have the same file naming system so http://jeff.scofer.com/thinkingstick/?p=10 on the old blog is http://www.thethinkingstick.com/?p=10 on the new blog.
What script do I need to make this transfer and where do I put it?
Thanks!
Jeff
Jeff –
As your URL setup is identical, you can bypass most of the code above. All you should need is:
$requestedPage = $_SERVER[‘REQUEST_URI’];
header(“HTTP/1.1 301 Moved Permanently”);
header(“Location: http://www.thethinkingstick.com/”; . $requestedPage);
die;
I hope this helps!
Thanks Andy! I replaced the script in your original post with the script above and it seems to still redirect to the home directory when I visit a specific post. Example http://jeff.scofer.com/thinkingstick/?p=10
Also I’m working with your Durable 0.2.1 theme, which I’m loving. Where do I go to change the colors to make them permanent? I don’t want to give users the option of changing colors I just want to change them myself.
Thanks for your help and the theme!
Jefff, I’m not sure exactly, from what I can see, that code above should work. Perhaps your server doesn’t recognize the PHP $_SERVER variable?
With durable, just make your color changes, and then go into the wordpress admin under presentation>configure durable. You can then disable the color selector and your colors will stick.
Thanks Andy! Looking forward to your next theme!
One word of warning with the 301 redirect: be patient. Google, for one, doesn’t update pageranks that quickly, so you may see your new url as zero for a while. Also, pagerank is only one part of their actual ranking algorithm these days.
yes,you make a nice job really!Maybe I should do that like you