O'Reilly

Change Category Slug within WordPress

By on  

When I first started this blog, I set the category "slug" to "sugar."  Why sugar?  Because a lot of a good thing is candy;  thus, a bunch of MooTools tutorials is as sweet as sugar.  While I get a giggle out of calling my categories sugar, Google doesn't find it nearly as funny, nor does Yahoo or Bing.  In an effort to increase my SEO, I wanted to change my category slug to "tutorials."  That's where the awesome Redirection plugin and a simple regular expression comes into place.

Changing the Slug

You can very easily change the category slug within WordPress's admin panel by navigating to Settings >> Permalinks:

WordPress Category

I changed the slug from "sugar" to "tutorials."

WordPress Redirection

So why do we need the Redirection plugin?  Because search engines like Google and users who have bookmarked your site will try to reach the old address.  Thus, we need an appropriate redirect to push them to the new address.  Not only do we need a redirection plugin but we need it to be reliable.  I've been using Redirection for years so I knew this would be the best route.

While Redirection allows you to make a simple URL to URL redirection, I needed something a bit more powerful since almost anything could be after "/sugar/" in the URL.  What I did was use Redirection's regular expression capabilities to create a wildcard search on the URL to redirect all "/sugar/" URLs to "/tutorials/" URLS:

 WordPress Redirection

And there I have it;  all category URLs are redirected as they should be! The (*) in the source URL grabs the text after "/sugar/" in the URL and the $1 in the Target URL places that text after the new category slug!

The Redirection plugin for WordPress is outstanding.  As I mentioned above, I've used it for years, have hundreds of redirections in place, and it's always been one of my most trusted plugins.  Thanks to John Godley for his great plugin!

O'Reilly Velocity Conference
Save 20% with discount code AFF20

Recent Features

  • CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • How to Create a Twitter Card

    One of my favorite social APIs was the Open Graph API adopted by Facebook.  Adding just a few META tags to each page allowed links to my article to be styled and presented the way I wanted them to, giving me a bit of control...

Incredible Demos

  • HTML5 Datalist

    One of the most used JavaScript widgets over the past decade has been the text box autocomplete widget.  Every JavaScript framework has their own autocomplete widget and many of them have become quite advanced.  Much like the placeholder attribute's introduction to markup, a frequently used...

  • Create a Clearable TextBox with the Dojo Toolkit

    Usability is a key feature when creating user interfaces;  it's all in the details.  I was recently using my iPhone and it dawned on my how awesome the "x" icon is in its input elements.  No holding the delete key down.  No pressing it a...

Discussion

  1. That works well if you’re changing category base. However, if you’re familiar with htaccess (granted most WordPress owners aren’t) and don’t want to use a plugin, you could do the following:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^sugar/(.*) tutorials/$1 [L,R=301]

    Furthermore, if you’re migrating your legacy category structure over to tags (via the admin) and want to prevent broken links, you could write a rule to handle the change:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^category(?:/[^/]+)*(/[^/]+) tag$1 [L,R=301]

    (Where ‘category’ was the old structure and ‘tag’ is the new structure. The regex handles sub-categories, which now become hierarchy-less tags.)

    • Yep, you can edit .htaccess directly, but that can be cumbersome for those who aren’t well versed with coding rewrites.

    • “/me likes simon post”

  2. I definitely prefer to use a plugin for something like this. My .htacess file is bloated enough.

  3. I think this solution is for one category… what if i want to change many category’s slug? help would be appreciated

  4. i have a big problem and maybe you can help me.
    site/category/apple and site/apple are showing me the same thing. how can i redirect the second one to the first one through htaccess.
    my post structure is site/apple/id/post/
    thanks

  5. Hi,

    Thanks for this!

    I recently changed my url structure from

    http://theadventurouswriter.com/quipstipsworkingworld/

    to

    http://theadventurouswriter.com/working/

    And now I need to reroute the incoming links so they go to the correct place.

    I tried inserting the urls themselves in the Redirection plugin’s source and target fields, but that didn’t work.

    Can you tell me what to insert in the source and target fields, to make this plugin work for me?

    Cheers,
    Laurie

  6. I am having the same issue as targoviste, As he said: “site/category/apple and site/apple are showing me the same thing” is this normal? How do I fix this?

  7. Лучший, популярный кинозал Mediavir.Ru. Смотреть онлайн и скачать новинки кино в хорошем качестве: боевики, фэнтези.

  8. Сайт объявлений о продаже, покупке товаров во всех городах России от частных лиц и фирм

  9. The best is to think wisely when you set up the site and stick to the link structure you’ve chosen. It’s almost impossible to redirect all the links Google already has indexed.

  10. That was exactly what I was looking for. Thanks for that! Luckily my site has not been up for too long so my category change hopefully won’t be too much of an issue.

  11. купить квартиру в подмосковье, продажа вторичных квартир в подмосковье, купить квартиру эконом в подмосковье

  12. Excellent and straight forward explanation. Thanks for the help!

  13. Good article, but the plugin consumes too many resources for redirection 301.

  14. I’m actually working on a massive category change for my site and this has been one of the best explanations of redirects I’ve read since researching the topic. Also thanks for the recommendation on the redirection plugin, I noticed you can also configure it to write the redirects you put in it into your htaccess instead of just wordpress. :)

  15. Thanks for this easy explanation David! I wouldn’t have been able to figure out the regular expression thingy without your clear example.

  16. Tom

    I have been searching for exactly this solution. Thank you so much!

  17. Works perfectly. Thank you very much!

  18. Thanks a lot for this post. Really help me out. By the way, you do have a post, where I can read more about how to edit the size of the category text. It will be great if describe it by editing the code in the Editor section. All the best and keep writing such a great posts.

  19. Asi

    Hi
    Is it possible to change category slug only for some categories? (for example I have 10 categories I want to change category slug for 2 of them, in other 8 categories I want no change in slug)

  20. Tom

    About the section “Changing the Slug”, it is not the category slug, it is the category_base conf. Category slug is the friendly version of a category name.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!

Recently on David Walsh Blog

  • Open Files from Command Line on OS X

    I'm as much of a fan of application UIs as anyone else but I'm finding myself working more and more from the command line lately.  Much of that is becoming obsessed with media manipulation but I'm forcing myself to use less UIs so that I...

  • Get Stock Quotes From Command Line

    When I conned my way into my first professional programming gig, I didn't really think much about money -- just that I was getting my foot in the door.  But as my career has gone on, I've been more aware of money, investing, and retirement.  I've recently...

  • Geolocation API

    One interesting aspect of web development is geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can...

  • Create an Image Preview from a Video

    Visuals are everything when it comes to media.  When I'm trying to decide whether to watch a video on Netflix, it would be awesome to see a trailer of some kind, but alas that isn't available.  When I'm looking to download a video on my computer,...

  • New:  Webdesigner News!

    A new and exciting website has recently been launched for web designers and developers. You likely spend hours every morning browsing through hundreds of posts on your RSS feeds, hoping to stumble across relevant stories. Webdesigner News was built to provide web designers and developers with...