With all the recent changes in this site I’ve been spending a lot of time learning about and digging around in .htaccess files. There is a lot of really neat things you can do in that file.
The one thing I wanted to share this evening was how to fix your old addresses if you update your permalinks in Wordpress. What Wordpress updates on its own is a little bit of a mystery to me. It updated for some permalink modifications (to a structure that included the category) but for others it did not.
I once upon a time included the month/day/year as well as the article title in my posts. This came from a permalink setup like the following: /blog/%year%/%monthnum%/%day%/%postname%/
I (for a variety of reasons) wanted to change to the following: /blog/%postname%/
After making the change I tried out an old address that was posted to Facebook
old: /blog/2013/01/02/tickle-age/ new: /blog/tickle-age/
But guess what… the old one came up 404 not found! NooOOOoOOoOOoOOO!!!!11!!!
I found and used a great tool over at Yoast (Wordpress SEO) to help me figure out the proper fix…
I ssh-ed into my server, opened up my root .htaccess file and added the following:
RedirectMatch 301 ^/blog/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/$ /blog/$4
What this is doing is a regex on the URL and redirecting via 301 (permanent redirection) to the same url with the year, month, and day removed. Works like a charm!
In the end, my .htaccess file looks like this:
-
- The below lines have been crossed out… keep scrolling for proper solution
`# BEGIN WordPress
RewriteEngine On
RedirectMatch 301 ^/blog/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/$ /blog/$4
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
END WordPress`</del>
Hope this helps!
** * Update: 15 January 2013 * **
So beware… if you follow the above steps, you won’t have a solution for long. Apparently I left out (and just learned) an important mod to the above steps. It should have been common sense, but I missed it. Lesson learned.
If you write any lines between the BEGIN and END Wordpress comments, it will most likely go missing.
Here is my current .htaccess:
` RedirectMatch 301 ^/blog/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)/$ /blog/$4
BEGIN WordPress`
RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
END WordPress
Notice how my addition is now on the outside of the comments. That’s the way to go. And just because I did one before, here is the proper screen shot.