RedirectMatch Fix for WordPress Permalink Change
WordPress is awesome, but I initially created a couple blogs with the url structure of mysite.com/2010/07/13/post-name/
The url structure worked fine until I starting thinking, “hey, those are pretty long urls.”
Doing some research, I found that having the year, month, and day in the permalink structure i.e., /%year%/%monthnum%/%day%/%postname%/ created other problems such as if you wanted to repost an old post to the front page with an updated date, links previously created to the post would be broken.
Also, what purpose does the year, month, and day serve in the url anyway other than creating long links, and letting the very few who would read the url know how old your content was.
I decided to go with a potentially more SEO friendly url structure for my wordpress blogs, but changing the link structure in the wordpress admin section would break internal links within my sites and external links from those linking to my site (especially since I had like two external links
Searching the Internet proved frustrating because all sorts of plugins were recommended to fix the problem, some of the plugins weren’t frequently updated, and did I really want a plugin working all the time when visitors access the page if I don’t have to?
I started looking at editing the .htaccess file when I found a post that discussed making the change with one line in the .htaccess file. This sounded like the fix I was looking for.
The only problem was that I couldn’t get it to work on my server. I thought perhaps the module for redirecting wasn’t enabled, and then I found out that the hosting company I was using doesn’t update changes to .htaccess immediately. Frustrating!
So, here’s what you can do to get the change to work:
If you’re not sure whether your hosting company supports RedirectMatch, do the following:
- Create an html file called test.php
- Upload the file to your server’s root directory
- Edit your .htaccess file (or create one if you don’t have one) and enter RedirectMatch 301 /test.php /index.php
- Visit yoursite.com/test.php and it should redirect to your home page
If you know your site will support RedirectMatch, then try the following:
- Copy and past a few of your old links with the /year/month/day/post-name/ from your wordpress blog into a text file
- Change the permalink structure to /%postname%/ in the permalink settings
- Add the following to the very top of your .htaccess file:
RedirectMatch 301 /[0-9]{4}/[0-9]{2}/[0-9]{2}/([^/]+)/ /$1/
What this code does is an auto-match based on the Regular Expression above to redirect traffic from the old link structure to the new. The 301 indicates to search engines that this change is permanent.
The /[0-9]{4}/ represents the year value (0-9 are the character possibilities repeated {4} 4 times), the rest follows for the month, and day.
The /([^/]+)/ represents what would be in the post name
The /$1/ represents the postname ([^/]+) in the previous section, so you’re replacing the /year/month/day/post-name/ with just /post-name/ if someone tries to access the old link structure.
I’m not an expert at regular expressions, but it’s worked so far. Give it a try if you’re running into the same issue.






