Fixing Your Ugly Permalinks With a 301 Redirect

Ugly permalinks are bad
Warning: Applying this may cause performance issues.
Read the structure tags section here for more info.
If you are reading this, you are most likely looking for the solution to get rid of your current permalink structure without breaking the backlinks to your blog. First let’s discuss what permalinks are.
Permalinks are the permanent URLs to your individual weblog posts, as well as categories and other lists of weblog postings. These are the links coming from search engine traffic as well as from other sites. The URL to each post should be permanent, and never change — hence permalink.
The default looks like:
http://www.profwebmarketing.com/?p=123
Then you have “pathinfo” permalinks:
http://www.profwebmarketing.com/2009/03/02/sample-post/
or
http://www.profwebmarketing.com/2009/03/sample-post/
The last 2 examples are better than the 1st, but for SEO and site usability we want to use either:
http://www.profwebmarketing.com/category/sample-post/ orhttp://www.profwebmarketing.com/sample-post/
I prefer the second example, but many prefer to have the category name included in the URL for added SEO benefit’s.
So You Started Your Blog 100 Posts Ago?
Maybe you didn’t know about SEO and site useability when you started your blog. Or you were like me and just overlooked the permalinks for a while and already had posts indexed in the search engines and linked to on other sites. What now?
Well with a quick change inside of your WP settings and a simple 2 lines of code added to your .htaccess file, we can change our permalinks without breaking any links we have coming in to our blog.
First, log in to your WP control panel and select “permalinks” under the settings menu.

Once you are on the permalink settings page, you will want to check the “Custom Structure” radio button.
If you want to use the http://www.profwebmarketing.com/category/post/ structure, you will enter /%category%/%postname%/ into the Custom Structure field.
And if you want to use just the post name in your url’s you would enter /%postname%/ into the field.
And thats it for changing your permalink structure. Now for the tricky part – Keeping the incoming links from breaking.
You will already have an .htaccess file inside of your blogs root directory. Download it and open it in notepad or some other plain text editor.
Now it depends on the structure of your old permalinks on what you will enter in your htaccess file.
In my case my old links were:
http://www.profwebmarketing.com/%year%/%monthnum%/%postname%/
So what I had to enter in my .htaccess file was:
RedirectMatch 301 /([0-9]+)/([0-9]+)/(.*)$
http://www.profwebmarketing.com/$3
If my old links were:
http://www.profwebmarketing.com/%year%/%monthnum%/%day%/%postname%/
I would have entered:
RedirectMatch 301 /([0-9]+)/([0-9]+)/([0-9]+)/(.*)$
http://www.profwebmarketing.com/$4
I’ll explain a little bit about what is going on here based on the last example I gave.
RedirectMatch 301:
301 redirect is the most efficient and Search Engine Friendly method for webpage redirection.
The first “/” looks at the root of your domain, i.e. www.profwebmarketing.com
Next we have RegEx or regular expressions:
1 2 3 4
([0-9]+) / ([0-9]+) / ([0-9]+) / (.*)%year% / %monthnum% / %day% / %postname%
%year% is a 4 digit number => [0-9]+ => [0-9] can only match a single digit but the “+” means that we don’t know how many digits this particuliar number has ( you could replace [0-9]+ by [0-9][0-9][0-9][0-9] if you knew it was 4 digits but using the + is much better. Same with the %day% and %monthnum%.
Then we have the last part of the 1st line:
(.*)$ will match anything that you have at the end of your URL, in this case %postname%.
The “.” in RegEx signifies any character (except \n newline). The “*” most know means wildcard or in regex terms, 0 or more of previous expression. So basically it grabs whatever your postname is.
Now the second line in the .htaccess is:
http://www.profwebmarketing.com/$4
This line is where your old links redirect to. The $4 at the end of the line grab’s the (.*) from the 1st line of the redirect which is your post name. If you were only using the %year%/%monthnum%/%postname%/ you would use a $3 at the end of your line telling it to grab the 3rd part of the redirect line.
Hope this helps! I’m no coder by any means, but I am learning and wanted to pass this along. The credit for most of this goes to JulienH who is a coder.




Mon, Mar 2, 2009
featured, tips