Before relaunching this blog last week I checked my Google Analytics data for the site. KevinMuldoon.com hadn’t been actively updated for the best part of two years but I knew it still got a little traffic and was curious as to where it was coming from.
The majority of links were from my author bios on other blogs though there were a few random incoming links to my previous blog posts too. I checked out a link to my site from Rawaii Muay Thai – a muay thai camp I went to 3 years ago to train. The page linked to https://www.kevinmuldoon.com/2009/01/12/first-week-in-thailand-at-a-muay-thai-camp/ however that page brought up a 404 error.
The reason this occurred is because I had at one point changed my WordPress permalink structure from day and name (/%year%/%monthnum%/%day%/%postname%/) to post name (/%postname%/). I recall setting up redirects at the time however I probably removed these later when I deactivated my blog and used an HTML template instead.
This obviously affected all posts that I had published before I changed the permalink structure so I had to look for a solution that changed URLs such as https://www.kevinmuldoon.com/2009/01/12/first-week-in-thailand-at-a-muay-thai-camp/ to https://www.kevinmuldoon.com/first-week-in-thailand-at-a-muay-thai-camp/.
I know that there are WordPress plugins available that redirect incorrect URLs however I prefer to do redirects directly via the .htaccess file. I found a fantastic script from Joost de Valk called the WordPress Permalink Generator that helps you do this.
All you have to do is enter the URL of your website and the sub directory it’s installed in. You can choose from the main permalink structures or enter your own custom permalink. The script will then provide you with the code you need to add to your .htaccess file.
Note, you don’t actually need to do redirect the default WprdPress permalink (i.e. Default ?p=123) as WordPress will redirect it for you.
For reference, here’s the code you need to add to your .htaccess file in order to redirect an old WordPress permalink structure to /%postname%/. Simply replace http://www.yourwebsite.com/ with the URL of your WordPress installation e.g. https://www.kevinmuldoon.com/, https://www.kevinmuldoon.com/blog/ or https://www.kevinmuldoon.com/wordpress/.
* I still recommend trying out the WordPress Permalink Generator
Day and Name (/%year%/%monthnum%/%day%/%postname%/)
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/(.*)$ http://www.yourwebsite.com/$4
Month and Name (/%year%/%monthnum%/%postname%/)
RedirectMatch 301 ^/([0-9]{4})/([0-9]{2})/(.*)$ http://www.yourwebsite.com/$3
Numeric (/archives/%post_id%)
RedirectMatch 301 ^/archives/(\d+)$ http://www.yourwebsite.com/?p=$1
Make sure you make a backup of your .htaccess file before you make any changes to it so that you can revert to the old version if anything goes wrong. Do not skip this step; one mis-placed character in the .htaccess file will shut down your whole website, so make sure you can overwrite a corrupt .htaccess file with a working version if anything goes wrong.
Hope you found this useful. Kudos to Joost de Valk for creating such a useful little script for free :)
Thanks,
Kevin