cc licensed ( BY NC ND ) flickr photo shared by The Rocketeer

This may be a niche case but I am just making note for myself. If you ever feel the urge to change the Permalinks setting on your WordPress site, keep in mind you will break any old external links that point to it.. meaning if for years you have publish posts as:

Changing to the more compact form enabled in recent updates, you can get away with a change that will make the sam post link:

Meaning we can clean up a few characters by removing the date/time cruft in the URL. This is fine for our blog, but anyone who has ever linked to that old post will now get some friendly 404 Not Found screen.

There is a fix, and I recently helped Giulia fix this on her blog. Some of her old posts I saw in the ds106 archives such as

were rendered not found as she changed her URL structure to be

I fixed all of her posts in one line of code (or codish) text – the magic is in the .htaccess file on the root of the server. This helps rewrite URLs to be more public pretty- actually wordpress urls look something more like:

where 245 is a database ID. The rules of htaccess allow web urls to be interpreted from how the browser sends them to what the server expects. It allows humans to use readable URLs and machines to use uglier ones.

The one I needed I found via 10 Awesome .htaccess Hacks for WordPress:

The first part instructs the web server to return a permanent redirect (301) to find anything that has the three numbered structure (e.g. yyyy/mm/dd/) and find all the stuff that comes after that (.*) – every set of patters in (…) corresponds to variables we can use in the result– $1 $2 $3 are the year, month, day respectively, and $4 is the string of the url we want to preserve.

Yeah, it looks like gibberish. But it is possible to clean up from a permalink change.

It just takes a magic hat.

The post "Fixing Permalinks with Some htaccess Magic" was originally scraped from the bottom of the pickel barrel at CogDogBlog (http://cogdogblog.com/2012/11/permalink-magic/) on November 20, 2012.

4 Comments

  • Tim Owens (@timmmmyboy) twitter.com/timmmmyboy

    I thought WordPress did this automatically? I feel like I’ve gone to URLs before that were of a different structure and it redirected. I guess not. Good to know…

    • Alan Levine aka CogDog cogdogblog.com

      Nope it doesn’t. WordPress always honors the …./?p=xxxx form but if you switch between any of the other permalink structures, it only honors the current one in use.

      It would not ake much to write a plugin that might compensate for any of the different standard permalink options.

  • Wesley Fryer speedofcreativity.org

    I read somewhere that longer permalinks which include the complete words of your post titles are better for SEO. Not sure if that is still true or not, but that is why I’ve stuck with the long versions and turn that on when I setup new WP sites. I like how you can still use the short link form (/?p=245) even when long permalinks are on, the short link just redirects. I’ve wondered if there is a way to make those short links the visible permalinks at the bottom of a post, but still keep the long form permalinks for SEO. There is probably an existing plugin to do this… Not sure if it would be worth using or not.

    What was your rationale for going with newer short permalinks?

    • Alan Levine aka CogDog cogdogblog.com

      @wesley- I am talking about the difference between 2 “longer forms” the one swhere the date elements are part of the url like this post

      cogdogblog.com/2012/11/20/permalink-magic

      versus making it

      cogdogblog.com/permalink-magic

      I do not really focus on SEO and I usually chop down the readable part of the title to a few key words. Taking the date info out saves a few chars.

      I did not change mine at all; I saw some students sites that had changed their permalink structure like above, and it made existing links not work anymore.

      Actually what you call the “short link form” is the actually raw wordpress form that the server sees (minus the assumed index.php), e.g. …./index.php?p=245 This one will always work despite permalink structure, because it is the actual URL. It does not redirect, in fact, it is wordpress’s own htaccess created that makes the other formats redirect (or actually be re-written) internally as the ?p=xxx form.

      O prefer the longer form as it presents info to someone who reads URLs; I can communicate the topic and its date rather than a database ID

      If you wanted to have that appear, you would add something to your template (could be made into a plug in with a hook into the content):


      <?php echo home_url('/') . 'p?=' . get_the_ID(); ?>

      I use a plugin called Twitter Friendly Links which does this
      http://wordpress.org/extend/plugins/twitter-friendly-links/

      so I have output at the top of my posts the url as (this post) http://cogdogblog.com/16623 which is a tiny bit shorter than http://cogdogblog.com/?p=16623

Leave a Comment

All fields are required. Your email address will not be published.