Removing Category Base from WordPress URLs

FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Removing Category Base from WordPress URLsMarch 1st, 2008

« Instruct Search Engines to come back to site after you finish working on itFaster Form Submission and Processing with fsockopen »

I received the following from James, basically requesting information on how to remove the /category/ base from WordPress urls.

Hey, man. Spent quite a while on the site and i’m still having an issue with this, so i’ll give it a go with you.

We’re using WP as a CMS with pages and posts. Done a bit of customization, so it looks sweet. Problem – i have some child categories that throw 404 when the category redirect is in place. How did you remove the category base from the url structure in wordpress?

This has really got me going… thanks for being accessible!

This was one of the first hacks I figured out for WordPress, so if anyone has an improvement…

WordPress Modifications

It’s very important that you replace all the links on your site to /category/slug/ to /slug/ or you will have problems. These steps should only take you a few minutes.

Removing /category/ Automatically

This is the main hack to make it work automatically throughout your site. You can place this code in your themes functions.php folder, or you can stick it in a plugin file.

function fix_slash( $string, $type )
{
    global $wp_rewrite;
    if ( $wp_rewrite->use_trailing_slashes == false )
    {
        if ( $type != 'single' && $type != 'category' )
            return trailingslashit( $string );
 
        if ( $type == 'single' && ( strpos( $string, '.html/' ) !== false ) )
            return trailingslashit( $string );
 
        if ( $type == 'category' && ( strpos( $string, 'category' ) !== false ) )
        {
            $aa_g = str_replace( "/category/", "/", $string );
            return trailingslashit( $aa_g );
        }
        if ( $type == 'category' )
            return trailingslashit( $string );
    }
    return $string;
}
 
add_filter( 'user_trailingslashit', 'fix_slash', 55, 2 );

category.php Theme Modifications

Here’s one way to fix the url in the category.php file of your wordpress theme.

<h1><a href="/<?php echo str_replace(' ','-',strtolower(single_cat_title("", false)));?>/"><?php echo single_cat_title("", false);?></a></h1>

.htaccess Modifications

The below .htaccess code illustrates 2 different ways to redirect any request for /category/slug/ to /slug/. You should only redirect after applying the php hacks described earlier, otherwise you could end up creating a loop.

RedirectMatch 301 ^/category/(.+)$ http://www.askapache.com/$1
# OR
RewriteRule ^category/(.+)$ http://www.askapache.com/$1 [R=301,L]

That’s it!

« Instruct Search Engines to come back to site after you finish working on itFaster Form Submission and Processing with fsockopen »

Reader Comments

  1. Mike Wolstat says:

    Do you put the .htaccess file in the wordpress install root folder? i.e. in the same folder as the sub-folders wp-admin, wp-content and wp-includes?

    thanks

  2. Sebastien says:

    no result, why ? /category/blog/
    (sorry i’m french)

  3. askapache.com commenter John Doang says:

    Does this work with child and grandchild categories?

    Doesnt work for me, so must be because I have child cats?

  4. askapache.com commenter Miren says:

    Hi! Same here, I added the code to functions.php (I have no category.php file on my theme), and now http://www.mysite.com/category/name redirects to http://www.mysite.com/name, but it loads “Not found”. I could not find any way to display the category page, any idea what could be going on?

  5. askapache.com commenter Dusty Reagan says:

    Hi! I tried adding that code to my functions.php with no results. I know that fix_slash is getting called, but it’s not removing the “category” directory. Any idea what could be wrong. Thanks!

RSS feed for comments on this post.TrackBack URL

Add Your Opinion

Skip to Comments
184

It's very simple -
you read the protocol
and write the code.
-Bill Joy

HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | WAI | DISA | ICSI | GIAC | SANS RR | GHOST

Authority: 110

↑ TOP

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed according to these terms. "Apache" is a trademark of The ASF.

Site Map | Contact Webmaster | Glossary | License and Disclaimer | Terms of Service