Tidbits on software development, technology, and other geeky stuff

301 Redirect When Changing WordPress Permalink Settings

When I originally setup my WordPress site I decided to go with the “Month and name” permalink option so that my post urls would include the date they were posted.  In hindsight, I wish I would have chosen the “Post name” option initially because this is now what I prefer.  Shorter urls are better.  If someone wants to know when a post was published they can easily find that on the post content itself and don’t need to have it shown in the url.

permalinks settings

But once I changed this setting my old post urls were resulting in 404 responses.  Posts linked from other sites and search engines now wouldn’t come up correctly because WordPress is hosting these at a different url.

I looked for a easy plugin to 301 redirect requests from my old format Urls to the new format (i.e. http://www.geekytidbits.com/**2011/05/geeky-post** to http://www.geekytidbits.com/**geeky-post**) but couldn’t find one that fit the bill.

In the end, I simply added the following RedirectMatch rule to my root .htaccess file.

<IfModule mod_rewrite.c>
  RedirectMatch 301 /\d{4}/\d{2}/(.*) http://www.geekytidbits.com/$1

If you previously had used the “Day and name” setting where the actual day of the month was included in the permalink you would want to use this instead:

<IfModule mod_rewrite.c>
  RedirectMatch 301 /\d{4}/\d{2}/\d{2}/(.*) http://www.geekytidbits.com/$1

If your .htaccess file contains a  # BEGIN WordPress … section, you’ll most definitely want to place this bit below the line containing # END WordPress.

Discuss on Twitter