Remove The Word Category From WordPress URLS

wordpress category removeWordPress shows /category within it’s url link output for categories, this however does nothing to help SEO and more so looks pretty ugly. Removing the category word from wordpress url structure is pretty simple and brianhoff originally wrote a short tutorial covering how to do this. Since v2.6 of wordpress was released however things have change slightly so we thought it would be nice to post an updated version of brians original solution with the correct instructions for v2.6 of wordpress and a lot more detail for beginners.

Understanding What You are About To Do

It’s always worth having a little background information about what your going to acheive by removing the word category from within the wordpress URL structure.

When you create a category in wordpress “Technology” for examples sake with SEO URL re-writing enabled and then browse to that category on your wordpress install the URL structure will look like below.

http://yoursite.com/category/technology/

You will notice the word /category within the URL structure, what this wordpress hack achieves is the removal of the /category word from all URLS. After implementation this then means that the URL structure for categories would now look like below.

http://yoursite.com/technology/

Much cleaner right and it also moves url keywords further to the left.

How To Remove Category From WordPress URL

Open the file /wp-includes/category-template.php in a text editor, we would recommend using PSPad which is a free code editor.

Around line 42 of /wp-includes/category-template.php

FIND

        $catlink = str_replace('%category%', $category_nicename, $catlink);

BELOW ADD

        $catlink = str_replace('category/', '', $catlink);

The code within the category-template.php should now look like the image below, the grayed out areas are the code you should have changed with line 43 being the code you added.

Save the /wp-includes/category-template.php file and upload it to your wordpress site.

Note: The following step is very important or your site will receive 404 errors for categories.

You need to tweak your sites permalink structure from the admin panel, this is done because all posts must have /%category%/ somewhere within the permalink structure. It’s also claimed to be a little better for seo as your urls will now contain more keywords further to the left of the url. This next step is done completely with the admin panel so no more file edits are required.

Goto your wordpress Administration page then Settings / Permalinks. You should be presented with a page like below.

The part we are going to change is Custom Structure so go ahead and select custom structure from the page. We are now going to fill in the text box with our new URL structure code, the image below shows what it looks like and we have included the code for you to copy under the next image.

Custom URL Code Snippet

Copy and paste the code below to your custom structure text field and click save.

/%category%/%postname%-%day%%monthnum%%year%/

What Is The Custom URL Snippet?

The custom code snippet above controls the way url’s are outputted on a wordpress site, below we explain a little about each part of the code.

%category% – This part of the code displays the category name of which the article belongs to.

%postname% – This part of the code is your actual post title.

%day% – Day numer 2 digits

%month% – Month 2 digit

%year% – Year 4 digit

Your resulting article/post url would look like below for category technology and article title “iPhone 3g Launches Around The Globe”

http://yoursite.com/technology/iphone-3g-launches-around-the-world-18082008

You can change any part of the url structure to suit your needs if you remove the %category% code though you will receive 404 category errors. All the tags you can use in wordpress URL structure can be found at the Using Permalinks page over at wordpress.org

You now have a wordpress install without the word /category in the URL structure.

Massive thanks to brianshoff.com for the original code snippet.

If you enjoyed this post, make sure you subscribe to my RSS feed!

Article Details

#

Author: on August 18th, 2008

Category: Wordpress

Tags: , , , , ,

  1. Salario says:

    Hi Lincoln,

    Thank you for providing this excelent tutorial on getting rid of the unwanted ‘category’ in WordPress URLs.

    I implemented this technique, and it does work, but on my webiste, I have categories nested within categories, and so when I try to go to a nested category page, I get a 404 error. Do you know of an additional work around for dealing with sub categories?

    Thanks again for hosting this site!

    Salar