theParagon

Redirect 301

Note: I wrote this to help me (or others) out later. More than likely this is more than what most of my readers want to know but I have a tendancy to write like this every once in awhile.

This is something I’ve been trying to figure out for the past couple days and for some reason it just hit me what I was doing wrong. So for anyone out there that may be having this problem - I thought I would pop this post out there for a little help.

The Problem

After redesigning my site and moving it to a new server, I decided to redo a lot of stuff with my site. I decided to add new sections as well as consolidate others. With this came a different site architecture with also means a different file structure.

Example:
An old post at http://www.theparagon.org/theold/00000340.shtml

That same post is now located at http://www.theparagon.org/archives/2003/11/03/if_2_worlds_collided_would_they_know/

Most people wouldn’t give a second care about this but I have a few people linking to old posts on my site and if I’m renaming all my files - all their links would break. Not only that but all those search engines that have been indexing my posts for the past couple years will all of the suddon have a ton of bad links in it.

If that’s not convincing for ya, just read Cool URIs don’t change written by Tim Berners-Lee - the inventer of the Internet.

The Solution

As you know - if someone were looking for one of my old post that was located at http://www.theparagon.org/theold/00000340.shtml, they wouldn’t be able to see it because everything has been moved around. How dissappointing.

So the question is - “How do I have my old links point to my new links?”. Not only that but the correct post. So if someone were to type in the above URL, it would be automatically redirected to http://www.theparagon.org/archives/2003/11/03/if_2_worlds_collided_would_they_know/.

The solution - Redirect 301. Apache’s mod_alias module allows you to map different parts of the host filesystem in the document tree and for URL redirection.

Long story short, I simply made a .htaccess file in the root of my site and put in:

Redirect 301 /theold/00000340.shtml http://www.theparagon.org/archives/2003/11/03/ if_2_worlds_collided_would_they_know/

Ok, well, that takes care of that one article but what about the other couple hundred that are left?

With that in mind I had a couple options. The option I chose was to use my CMS to automatically spit out a few lines for me. My CMS is Moveable Type (MT) and all I had to do was create a new Template and add the following code.

<MTEntries lastn="10000"> Redirect 301 /theold/00<$MTEntryID pad="1"$>.shtml http://www.theparagon.org/archives/<$MTEntryDate format="%Y/%m/%d"$>/<$MTEntryTitle dirify="1"$> </MTEntries>

I had MT repeat the same Redirect script over and over until it finished with all my posts. So now, if you find an old post or are linking to an old post of mine - you’ll be able to see the article. It doesn’t break.

Update

One thing I should point out in the redirecting (this stumped me for a bit). The first URL right after Redirect 301 needs to be from root. So it needs to be written like /filename/file.shtml. However, the second one needs to be written with the full URL http://www.domain.com/newfilename/newfile.shtml.

That little part tripped me up and actually brought my site down when it didn’t work.

posted on July 3, 2004 | 9:01 AM EST

1 Comment

Add your thoughts.

louise Says:

I founnd outside this site some great links

[url=][/url]

Posted at: July 12, 2007 1:39 PM

Post a comment










Remember personal info?

Step 1:


Step 2:

Step 3:

Flickr Photos (all/by location)

Great Things

Elevator Up 7dots

View Aaron Schaap's profile on LinkedIn