Moving Wordpress: Individual 301 Redirects with PHP

I recently decided to move part of an old blog to an independent site.

In order to maintain the meager search engine and referral traffic that the old pages had, I planned on using 301 Redirects. I figured that I would re-create each of the old articles (~10 total articles) on the new site and set up an individual 301 Redirect to send users and search engine spiders to the new location.

The problem, however, was that the standard .htaccess rewrite that Wordpress uses was conflicting with the 301 Redirects. For some reason, they just weren’t working – so I turned to a PHP-based solution.

301 Redirects In htaccess

Normally, you can handle a 301 Redirect with a .htaccess file. You can do this with a single line in your .htaccess file.

redirect 301 /path/to/old/file.html http://newdomain.com/file.html

The first part “redirect 301″ indicates the type of redirect. This tells the browser (or the search engine spider) that the page has permanently moved to the new location. HTTP has several other options. The 302 Redirect, for example, indicates that the move is temporary.

The next two parts indicate the old path to the file and the URL of the new location.

Normally, that’s all there is to it. However, Wordpress adds its own layer to the .htaccess file which makes your pretty URLs work. The URL that you request is really just a bunch of variables that tell Wordpress what article to pull out of the database and display in the template.

Although you should be able to get the two to work together, I couldn’t. The server was ignoring my 301 Redirect and going directly to the other rewrite rules.

Using PHP to 301 Redirect a Wordpress Page

The solution? PHP.

Creating a 301 Redirect in PHP is fairly simple. I’d read about it before, and just the other day I came across a brief article about it in one of my regular RSS feeds.

You use the header() function to send custom headers to the user’s browser. With these, you can tell the browser that you want to initiate a 301 Redirect. You can then tell the browser where to go to find the article.

header("HTTP/1.1 301 Moved Permanently");
header("Location: http://newdomain.com/article.html");

Simply replace the URL with the URL you want to redirect to.

Making It Work With Wordpress

If you look in your site’s directory structure, you may realize that the path to your article doesn’t really exist.

Let’s say we want to redirect the article located at http://www.earn-web-cash.com/2008/03/20/useful-wordpress-plugins/. To use PHP, we’d need to create a PHP file to replace the article that is being redirected, and include the snippet of code above.

The problem is that the directory /2008/03/20/useful-wordpress-plugins/ doesn’t exist. Those are parameters sent to the Wordpress script to fetch information out of the database.

To make this type of redirect work, we need to create this directory structure. Start at your root, and create the directory “2008.” Create a directory “03″ inside that. Create a directory “20″ inside that. Finally, create the directory “useful-wordpress-plugins” inside that.

You’ll notice that there is a trailing slash in the URL. In a normal directory structure, “useful-wordpress-plugins” is a directory, even though it would seem to represent the file where your article resides. To get the browser to access a PHP file at this location, we place an index.php file in that directory. Then, accessing the URL will automatically pull up index.php.

In short, create /2008/03/20/useful-wordpress-plugins/index.php, add the snippet of code above, and your redirect should work flawlessly.

This is a good way to move a few articles on a site to a new site. If you’re moving an entire site, you’d definitely want to look into using the .htaccess method, or creating a PHP file that dynamically chooses what page to redirect the user to. I only had to move 10 articles, so I created ten php files to do the work for me.

There is one caveat to keep in mind. If your installation of Wordpress uses the URL http://www.earn-web-cash.com/2008 to access the yearly archives, you’ll run into a problem. /2008 will now be an actual directory – so the server will try to serve up the directory. You’ll need to change your settings so that the archives are located in something like http://www.earn-web-cash.com/archives/2008.

Bookmark and Share:
  • Digg
  • Furl
  • del.icio.us
  • StumbleUpon
  • MisterWong
  • DZone
  • Technorati

Tags: , , , ,

2 Comments to “Moving Wordpress: Individual 301 Redirects with PHP”

  1. Spread the Love: Intersite Linking | Web Cash said this on

    [...] I recently started working on a new website – the one I mentioned in the article on creating individual 301 redirects for Wordpress. [...]

  2. butzi said this on

    You could shorten your 2 commands for the redirect to just one:
    header(“Location: http://newdomain.com/article.html“, true, 301);

    The true tells the function to overwrite the header and the 301 to send the 301 code.

    The above way is somewhat hard to use, when you have more than 10 sites. It is more easy to use a custom 404.php. In that script you could check the $_SERVER['REMOTE_ADDR'] against an array with your old addresses, that should be redirected.

Leave a Reply