How To Change URLs

July 30, 2006

What do you do if you want to change the structure of your URL, but at the same time, want to maintain the integrity of your site by still accepting the old URLs? Read on for more information.

WordPress has an option for “Pretty Permalinks“, which is not too bad and what I had used for quite a long time. Consider the structure of a “pretty permalink”:

Personally, I feel this is quite long and if your post name has a lot of words, it can easily go beyond the character length that most emails accept before they wrap the url and the non-swavvy tech person on the receiving end complains to you that your link doesn’t work.

Since most blog posts tend to have the post date somewhere on the page, I felt the same information listed in the URL to be kinda redundant. I think the key component of the URL is the post name. To me, that’s the most important and I want to retain that information. I entertained the following options:


I was quite intrigued by the first option of listing the category in the URL, but after some experimentation, it could be quite long as the subcategory will also be listed. or

While this would be quite descriptive at it not only lists the post name but also all categories, I have the tendency of changing my categories around from time to time and technically, it would be quite difficult to map the “Pretty Permalink” to this category-based one.

The second option seemed quite nice as it lists the year the post was written in the URL. While this may not be as descriptive as including the month and day, I think it’s a good trade off. By only listing the year, you can shave off a total of 6 characters in the length of the URL. It may not seem to be that much, but personally, I consider the URLS to look a bit nicer.

But what about the third option? Why not remove the date completely from the URL? I fear that over the course of time, in my laziness, I may write two post with the exact same title. Plus, just seeing the post name after the domain looks like it’d be a directory of the webpage, IMO.

So, I decided to go with option 2 and leave only the year in the URL. Ok.. cool.. now how do I go about doing that and maintain the integrity of my old links? The answer involves playing around with your htaccess file and adding some URL rewriting rules.  For the quick and dirty way to achive the above (note, this is most likely not the most optimized approach), I used the following:

RedirectMatch 301 /([0-9]{4})/[0-9]{2}/[0-9]{2}/(.*)$1/$2

The above code has to all be on 1 line in the htaccess file with a space separating the “(.*)” and “http:…”.  Basically, this stores the 4 digit year of a URL into a variable $1 and the post-name in a variable $2 that can be used to redefine the format. What this means is that any link with the following structure

Will get redirected with a code of 301 (aka permanent) to the following structure

And best of all, I’ve cut down my URLs lengths by 6 characters, I have nicer viewer URLs, and all of this is done behind the scenes without any user intervention!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: