One of the things I like about WordPress is the ability to have pretty SEO friendly URL’s. One of the things I do not like is when people use the date as part of their permalink and here’s why:
- It dates your items, so re-posting on Twitter or FB can look out dated
- In the search engines your page name is now 2 directories deep which gives it less weight
- If you were to edit and update your post you still have the old date with new content
- If you were to re-date it, old links will now 404! Not something you want to do
As you might notice I use the /%postname%/ only in my permalinks. I have had to modify other people’s websites from the /%year%/%month%/%postname%/ to just the /%postname%/ and have found that there is no internal way for WordPress to Redirect 301 these pages.
I am not a fan of adding additional plugins if I don’t have to. Each additional plugin that is on your site increase the load time, memory, and processor usage. Since I want to have fast loading websites, I shy away from using plugins whenever I can.
Htaccess To The Rescue
I have to preface this by saying that in all examples I am not showing the first line, since it is generally in all existing .htaccess files, and it just makes the examples cleaner:
RewriteEngine On
Next I must say to make sure that these examples are before the WordPress:
# BEGIN WordPress
With that said:
The .htaccess file allows us to take care of this and many other situations very easily using the Redirect command:
Redirect 301 /2011/03/this-is-my-page/ http://www.mydomain.com/this-is-my-page/
However this is a lot of work if you have more than 1 page.
RedirectMatch 301
I am very fond of the RedirectMatch command because unlike Redirect, RedirectMatch has and creates variables to enhance its functionality. Here are a few examples, starting with the the WordPress Permalink Renaming fix that you are probably here for:
RedirectMatch 301 /20(.*)/(.*)/(.*)/$ http://www.mydomain.com/$3
In this example we are transforming the items in (.*) to variables: $1, $2, $3. We can then use these variables, or not. The $ after the last (.*)/ just tells the parser that this is the end of the area we are parsing.
As you can tell I am only using the last variable $3 and disregarding $1 and $2. Additionally I could have done it as:
RedirectMatch 301 /20(.*)/(.*)/$ http://www.mydomain.com/$2
But this could lead to a problem if I had a parent page or category named /20-tips-and-tricks/ with a post named /utilizing-canonical/, or:
http://www.mydomain.com/20-tips-and-tricks/using-canonical/
Other RedirectMatch Examples
Here are a few other examples to make more sense of RedirectMatch.
The safest way to Redirecting the WordPress Permalink for Day and Name -> to the Post Name only:
RedirectMatch 301 /20(.*)/(.*)/(.*)/(.*)/$ http://www.mydomain.com/$4
Another way depending on your actual posts:
RedirectMatch 301 /20(.*)/(.*)/$ http://www.mydomain.com/$2
Redirecting the WordPress Numeric -> to the Post Name only:
Sorry, but this not possible since we don’t have the link name
Redirecting anything from a previous /blog directory or WordPress install -> to the root website:
RedirectMatch 301 /blog(.*)$ http://www.mydomain.com/
Redirecting ALL posts from the category cats -> to the new page page dogs:
RedirectMatch 301 /cats(.*)$ http://www.mydomain.com/dogs/
Redirecting posts from the category cats -> to the new page page dogs:
RedirectMatch 301 /cats(.*)$ http://www.mydomain.com/dogs/$1
post_author_posts_link