« Instruct Search Engines to come back to site after you finish working on itFaster Form Submission and Processing with fsockopen »
Removing Category Base from WordPress URLs
March 1st, 2008
Contents
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="http://www.askapache.com/<?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!
Reader Comments
-
u could use this plugin: WP No Category Base
-
Awesome fix, and one to be kept on hand, but Andrew is right - I set the category slug to . and problem solved... (I wish all my redirect issues were so simple!)
-
You could also set you Base Category to ".", isn't it man? If you set like this at the beginning the problem is solved, am I right?
-
I haven't tried other tricks but the .htaccess one works for me. Thanks a lot.
-
Thank you for sharing the solution. But as I don't know much about PHP programming. I would like to choose use a Plugin to achieve the same result. It called " not category base plugin" http://wordpress.org/extend/plugins/wp-no-category-base/
-
beauuuutiful !! Works like a charm for one-level-deep categories. Thanks a lot!
-
Are this methods work with wordpress 3.0.1 ?
-
nice I was using (./) without braces, but after upgrading to version 3.0, it doesn't work anymore, so fix_slash function was the best solution. I only modified functions.php and it works again like a charm
-
nice I was using "./" without quotes, but after upgrading to version 3.0, it doesn't work anymore, so fix_slash function was the best solution. I only modified functions.php and it works again like a charm
-
Man, This is really working. I was using "./" ( without quotes ) as my category base to remove it, and it was working fine before I upgrade to wordpress 3.0 . But after upgrading , this "./" ( again, without quotes ) doesn't work anymore, so , fix_slash function was the only and the best solution I found. BTW: I only modified functions.php.... Only! My htaccess file is wordpress default htaccess file. I mean, I didn't make any other modification you mentioned, just added the fix_slash fucntion, and Voilaaaa, it works. Thanx dude.
-
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-contentandwp-includes? thanks -
no result, why ?
/category/blog/(sorry i'm french) -
Does this work with child and grandchild categories? Doesnt work for me, so must be because I have child cats?
-
Hi! Same here, I added the code to functions.php (I have no category.php file on my theme), and now www.mysite.com/category/name redirects to 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?
-
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!
