Fixing Permalinks with Some htaccess Magic

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.

If this kind of stuff has value, please support me by tossing a one time PayPal kibble or monthly on Patreon
Become a patron at Patreon!
Profile Picture for cogdog
An early 90s builder of the web and blogging Alan Levine barks at CogDogBlog.com on web storytelling (#ds106 #4life), photography, bending WordPress, and serendipity in the infinite internet river. He thinks it's weird to write about himself in the third person.

Leave a Reply

Your email address will not be published. Required fields are marked *