Redirect category postname to only postname in WordPress
Previously it was not recommended to use only the
/%postname%/ for the WordPress permalink structure because it was too resource intensive. But after the update to WordPress 3.3, I see that WordPress has added this as the default option and obviously many serious wp people recommends this structure as seo friendly!
As we were using this permalink structure
/%category%/%postname%/ on our site, it became sometimes difficult to change the post from one category to another after publishing, because the published url got the category fixed with the url. Also in the case of posts with multiple categories the category name was always chosen in alphabetical order in the url, not as the author’s personal preference. I thought lets change our blog posts to this new structure, then it would be easy to maintain/change the categories.
So I went ahead and changed the permalink, but WordPress itself couldn’t take care of the url redirects in our live site (why I don’t know, because I tested this in localhost too and this worked, may be conflicting with some plugins here) resulting in lots of 404s for our old urls.
If you want to use any of the codes below just replace “/blog/general/” with your /parent-category-name/child-category-name/
Simple but time consuming
Save me .htaccess! Tried to add these in the site root’s .htaccess file. But after adding a few, I got tired. Adding so many .htaccess redirects for all these posts individually (like this code below) would be a real trouble and time consuming too.
redirect permanent /blog/general/goodbye-ie6-join-countdown-to-history/ http://eyedealab.com/goodbye-ie6-join-countdown-to-history/
With a popular plugin
What else can I do…?? Go for the PLUGIN – A quick “Muskil Asan”!
So I just installed the “Redirection” plugin.
Chose the following options:
a) “Redirect to URL” by matching “URL only”
b) Source URL:
c) Target URL:
d) Regular expression (regex):
[✔] keep it "checked" (This is the important part otherwise the regular expressions placed in source/target won’t work)
I did only one instance for each category, and all our old links redirected to our new links properly for each post, giving me some opportunity to relax for the time being.
But I don’t like installing so many plugins… And just to avoid broken links I now have to keep this plugin always on, made me annoyed.
Back to .htaccess in a different way
Now seriously I wanted to convert this whole process to either my theme functions.php with some WordPress rewites etc or the .htaccess, and worked on it for next one or two days, ultimately coming to a satisfactory solution mentioned below.
I had to put this code in the .htaccess file
RewriteRule ^blog/general/(.*) http://domain.com/$1 [R=301,L]
or you can put it like this
RewriteRule ^blog/general/(.*) index.\php/$1 [R=301,L]
# BEGIN WordPress
RewriteEngine On RewriteBase /
Wow this worked! Now I am happy to get rid of the redirection plugin.
Although in the back of my mind I am still finding the reasons why the default WordPress canonical redirect did not work? But that will be another story…