Using Search Regex to Remove Dates from URLs in Internal Links
Say you’ve changed your permalink structure from /2010/05/25/post-title to plain old /post-title/ but all your internal links are still to the old format. How do you fix this? Well, you can (and should) set up a 301 redirect to remove dates from incoming links so that people clicking on links to your site will end up on the right post.
But you can also use the Search Regex plugin to replace the structure in all your internal links so they look better and aren’t dependent on the redirect. Here’s how.
(If you just migrated from Blogger, use this post to fix Blogger permalinks first, then come back to strip out the dates.)
Before using this tutorial run a database backup. Neither Search Regex nor I accept liability for use of the plugin/tutorial.
Removing Internal Links to Year/Month/Day
One of the most commonly-used permalink structures is year/month/day (i.e. http://blogcrafted.com/2010/05/25/post-title/) so I’m starting with that.
In “Search Pattern,” paste:
|domain.com/(\d*)/(\d*)/(\d*)/|
(be sure to put your own domain where domain.com is!)
In “Replace Pattern,” paste:
domain.com/
click “Regex,” but you can ignore the other little boxes that then show up.
Click here to see what it should look like.
First, click the Replace button and scroll down to see if it looks right. If it does, then scroll up and click Replace and Save or else it won’t be permanent.
Removing Internal Links for Other Date Configurations
So here’s the secret to how that code works. Every (\d*) is a series of numbers. So what we’re telling Redirection to do in the previous section is to look for three series of numbers of any length, followed by (.*), a series of characters of any length and to redirect that to just the 4th item in the series, namely the characters.
There are other ways to achieve the same goal. We could set the numbers at (\d{4}) for the year & (\d{2}) for the month & day, since a year has 4 numbers and months and days have 2. But we don’t need to because it should only match links to urls from your site with dates in them. This code also ignores the post-slug, but that’s ok.
So if you were only using two dates in the old Permalink structure, just use:
|domain.com/(\d*)/(\d*)/| and replace again with domain.com/
and if you were only using one date (like the year) in the old Permalink structure, use:
|domain.com/(\d*)/| and replace with domain.com/
Again, don’t forget to click Replace and preview it and then click Replace & Save to actually save it.
If this is more complicated than you’re comfortable with, this is also a service I offer, so contact me if you need someone else to do it for you.

Comments on this entry are closed.