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/
Process 1
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/
Process 2
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: /blog/general/(.*)
c) Target URL: /$1
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.
Process 3
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]
right after
# BEGIN WordPressRewriteEngine 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…
could you give me some advices to redirect from postname to category post name?
i used permalink /%postname%.html then i changed it to /%category&/%postname%.html. but since i am not familiar with redirect code 301 on htaccess so i haven’t changed the htaccess yet. and now the effect is i have tons of duplicate title on google webmaster, for old link, it directs to 404 not found page and the worst thing is i lost my traffic
please help me as soon as possible, thank you
Generally WordPress automatically handles this, maybe in your case it didnot happen
But unfortunately with regular expressions in .htaccess this cannot be done easily, because the server doesn’t have the idea which category your blog post is in.
So if you don’t want to loose traffic – you are left with two long options, either manually redirect individual links with .htaccess as mentioned above in post Process 1, or use the wordpress Redirection plugin and add the old links and new links manually there.