@madpilot makes

WordPress Hack: Changing your permalink structure without upsetting Google

WordPress has the ability to generate permalinks, which is great for Search Engine Optimisation. But what can you do if you need to change between them? Changing them in WordPress isn’t a problem – you go to the “Options” tab, click permalinks, and select a new one. However! If others bloggers have linked to your posts, or a search engine has already indexed your blog, their links will break.

With a little bit of .htaccess trickery you easily* change between the different options without breaking your old links!

Why the star next to the “easily”? If you have a lot of posts, it could take a while, but read on….

Out of the box you have three options (There is a fourth, but if you use that option, you probably don’t need this guide!):

The WordPress permalink options

In terms of SEO, The best option is Date and name based – having the title of the post should give a higher ranking. Next best is Numeric, but only because it gets rid of the &p=123 part from the URL. Luckily moving from Default and Numeric to Date and Time is easy!

If you are moving from Default to one of the other options, then there is nothing else to do – WordPress automatically responds to this style of this URL regardless of what option is selected.

There are two ways of moving from the Numeric Option to one of the other options – the quick way and the right way!

The Right way

The right way uses a permanent redirect (using Apache’s mod_alias module) for each blog entry – this will tell search engines that the page doesn’t exist any more and that they should index the new page instead. Unfortunately, this doesn’t update links of other peoples pages, so you will need to leave the hack for the life of the blog.

Open up the .htaccess file and add the following BEFORE the # BEGIN WordPress line:

  1. <ifmodule mod_alias.c>
  2. RedirectPermanent /blog/2007/02/05/this-is-my-blog-post /blog/?p=123
  3. RedirectPermanent /blog/archive/123 /blog/?p=123
  4. <IfModule>

You will need to dig into the database to find what ID number corresponds to each post. This is tedious even for a small number of posts, so I prefer the quick way. The IfModule line check to see if the mod_alias module is installed. The first RedirectPermanent link shows an example of changing from the Date and Title option. The second line shows how to change from the Numeric option.

The Quick way

The quick way uses Apache’s mod_rewrite module to rewrite the URL – as such it will only work when converting FROM numeric mode. If you need to convert from Date and Name to numeric, you have to user the Right way. Drop the following code BEFORE the # BEGIN WordPress line:

  1. <ifmodule mod_rewrite.c>
  2. RewriteEngine On
  3. RewriteBase /blog/
  4. RewriteRule ^archives/([0-9]+) /blog/index.php?p=$1
  5. <IfModule>

The IfModule line makes sure mod_rewrite is enabled, the next link tells Apache to turn mod_rewrite on. The RewriteBase line tells apache to automatically prepend /blog/ to all rewrite tests. The second last line is the meat and potatoes: it tells apache to rewrite any url that looks like /blog/archives/[one or more numbers] to /blog/index.php?p=[the numbers].

I would highly recommend using the Date and Name option – thankfully converting to that option is the easiest to do!

6 comments

  1. Thanks for this Myles, although just want to point out that the WordPress Manage Posts page shows the ID numbers next to the posts - saves having to go into the database to get IDs :-)
  2. Thanks for the help.



    I wasn't paying attention and had my blog set to the default option. If I change my permalink structure to date and name based, my local page links and rss won't work. Is there away so that wordpress will automatically update filenames to the new structure?
  3. WordPress won't update your local page links - you could write a script to do it, but don't forget that the old URL structure will still work, so you don't really NEED to update all the links.



    Same goes for RSS.
  4. Hi there. I foolishly started with the numeric permalink system and now 200 entries later I regret it. However, I can't find a way to migrate to the date and name option. I put this code before the Begin Wordpress:





    RewriteEngine On

    RewriteBase /front/

    RewriteRule ^archives/([0-9]+) /front/index.php?p=$1





    Note that in the closing tag I added a / where in your original example I didn't.



    I know how to do this the 'right way' but I really don't want to take the time to do 200 entries... any ideas?



    Thanks
  5. Hi Tony,



    The procedure is the same. Just make sure you selected Name and date in the Permalink structure admin screen (Under Options - Permalinks).



    Drop in the the redirect code above in to you .htaccess and you should be fine.



    Note that as I mentioned, you can move from one option to another and wordpress will work - this technique is only to ensure that the permalinks still work correctly.
  6. Hi, Thanks Myles. Between my comment and seeing yours, I found a mod that finally did the trick: http://www.deanlee.cn/?p=111&cp=10#comments



    Someone posted a fix to a security flaw in that mod which can be tracked down in the comment section. It seems to be working now, but if something breaks, I'll be back here. Thanks for your time.

Leave a comment