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.

permalink-settings

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 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…

Last modified on September 15th, 2015 at 8:36 pm.
Share this post:

Priyanka Naskar Majumder - the Owner, Eyedea LAB. Graphic & Web Design Studio, lives in Kolkata, India. Interested in: Usability research, Graphic Design, Web Design & Development.


8 Comments

  • A good tutorial.
    I have one question.
    I want to change the permalink from /%category%/%postname%/ to /%postname%/
    For example, I wrote a redirect
    RewriteRule ^blog/travel-reports/north-america/canada/(.*) index.\php/$1 [R=301,L]

    Perfectly redirect post URL from the
    http:// domain.com/blog/travel-reports/north-america/canada/toronto-3-x-the-best/
    to http:// domain.com/toronto-3-x-the-best/

    However, I also have a page that displays all posts from the category “blog -> travel-reports -> north-america -> canada”
    http:// domain.com/blog/travel-reports/north-america/canada/
    This, unfortunately, has redirected to address http:// domain.com/

    Is it possible to redirect all posts exclude same category?

  • Hi Sir/Madam,

    I have website http://www.technologyace.com and my urls are http:// technologyace.com/mobile/htc-one-phone-details
    http:// technologyace.com/cameras/example-posts/

    i dont want /mobile/ or /camera/ , /category/ in the urls. So how i will use this method. As i didnt get what is this /blog/general/

    Could you please tell me what will be the code for my .htaccess.

    thanks

  • Hi
    this was a very helpful article, I was wondering if you’ve had to deal with a broken link when it comes down to linking the specific categories on the website.
    On the site that I am working, the categories are linked in this format:
    “category/blog/general/” and after I add “/blog/general/” into the redirect, the former link doesn’t work on the website.

  • 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.

      • I believe this is one of the very old posts,but after wordpress 3.5.2 updates and above, you can simply redirect from website/category/postname to website/postname by simply using “Postname” settings in permalinks options and the vice versa by using the Custom Permalinks as /%category%/%postname%/

        Cheers!!


Leave a Reply

Your email address will not be published. Required fields are marked *

*

Allowed HTML tags: <strong> <em> <pre> <code> <a href="" title="">