« 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
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…
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.
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 );
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>
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 it
Faster Form Submission and Processing with fsockopen »
The love of liberty is the love of others; the love of power is the love of ourselves.
-- William Hazlitt
The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. Tim Berners-Lee
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-content and wp-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!
Tags: 301 Redirect, 404 Not Found, Apache, askapache, Htaccess, PHP, Port, post, Redirect, Rewrite Tricks, rewriterule, SSI, WordPress,
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
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. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group.
POSIX ® is a registered Trademark of The IEEE.
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