admin

How to Remove the Category Base From WordPress URLs

Share and Enjoy

    A lot of WordPress themes automatically show a list of the tags and categories which were assigned to a page or post.

    Great for the search engines and users finding groups of posts they might be interested in but with one very annoying consequence.

    The addition of /category/ into your URL as below:

    how to remove the category base from wordpress urls

    It looks messy, cheap, longer means harder to remember and adds no value whatsoever.

    This is a technique I use on most of the WordPress sites we build in order to remove the category base from the site URLs and acheive this instead:

    remove the category base from wordpresss urls

    Here is the solution:

    1) In your wp-includes/category-template.php file search for the following line of code:

    $catlink = get_option(‘home’) . user_trailingslashit($catlink, ‘category’);

    Directly below that, insert the following line of code:

    $catlink = str_replace(‘/category’, “”, $catlink);

    2) In your wp-includes/classes.php file search for the following block of code:

    if ( isset($error) )$this->query_vars['error'] = $error;

    Just below that, insert the following:

    if (@$this->query_vars["name"] == “page”){$tmp_ar = $this->query_vars;$this->query_vars = array();$this->query_vars["paged"] = str_replace(“/”, “”, $tmp_ar["page"]);$this->query_vars["category_name"] = $tmp_ar["category_name"];}

    3) Finally, in your .htacces file, add the following to redirect your old /category/ URLs to your new ones (if your blog doesn’t reside in the root, you’ll have to tweak it):

    RewriteRule ^category/(.+)$ http://www.domain.com/$1 [R=301,L]
    1. How to Preserve Google Analytics URLs in a PDF
    2. Quickly Remove White Spaces in Dreamweaver After Pasting from Microsoft Word
    3. 10 Best WordPress Plugins in 2010
    4. Web Design Tutorials: Create a WordPress shortcode plugin
    5. 39 WordPress Benefits
    6. November Presentation: Extended Uses of WordPress as a CMS

    Comments

    1. Phil says:

      Super useful, thank you!

    2. EM says:

      Is it possible to remove the search base safely? I tried to remove the search base by modifying the search_structure with $wp_rewrite command. But then even the posts are redirected to the search page. Any idea?

    All comments are manually approved so please don’t waste your time or ours with spam

    Get Involved!

    *