Creating 301 redirects between many pairs of URLs

I recently upgraded a website. The website consisted of static html documents and some simple php scripts. I transformed it into a wordpress blog. In this process every url was changed. In order to keep the pagerank and not break any inlinks I created 301 redirects between them.

If there had been an obvious pattern between the old and new urls this would have been a simple task.
For example lets say urls of the pattern:

should be changed into:

Then I could just have used this in my .htaccess file on website1:

But in this case there where no obvious patterns. Some of the old pages where converted to wordpress pages, some to wordpress posts and some to a wordpress category with the old text as the description for the category. All this resulting in a wide variety of new urls.

I solved the situation like this:
First i used a sitemap generator to get a list of all the urls that I needed to rewrite. If you google for “sitemap generator” you will find one. Myself I used http://www.xml-sitemaps.com/. After your sitemap is generated choose the alternative “Download Sitemap in Text Format” and you will get a txt file with the urls. It might look like this:

urls.txt

Now after each url add the corresponding new url you want to redirect to (sepparate with space). It might look like this:

urls.txt

Next I created this python3 script to generate the content for my .htaccess file:

By | 2011-06-21T11:17:01+00:00 June 21st, 2011|Uncategorized|0 Comments

Leave A Comment