I have a permalink issue. For your information, there are a few different categories within a website and there are many posts in a single category.

My permalink setting is:

Custom Stucture: /%category%/%postname%/

Category Base: .

After this setting saved, my url will be

www.mydomain.com/category-slug-name/

instead of

www.mydomain.com/category/category-slug-name/

I have set the Blog pages show at most: 8 posts under the Setting > Reading. The pagination will appear when there are more than 8 posts in a category page.

I clicked on "page 2", and noticed that www.mydomain.com/category-slug-name/page/2/ brought me to 404 error page.

Originally, the url of any category "page2" is www.mydomain.com/category/category-slug-name/page/2/, it works fine if there is "category" within the url as original url. Or else, it would be 404 error page.

My main concern is, I don't want the category appear in the url. But i want the slug name of category appear in url when i viewing the selected post. And "/page/2/" and the rest will work fine.

Hopefully that this issue is not going to be resolved with recommended plugins. Because, I don't want the website to be too heavy.

Any suggestions?

up vote 10 down vote accepted

That is totally normal behaviour. If you want to remove category base you will need to write some custom rewrite rules, not simple rules I must say. From my point of view, removing category base requires a extra job in each request that is worthless because it doesn't provide any advantage for SEO or for better site navigation. Some popular plugins, like WordPres SEO by Yoast, had this option in the past and removed it some time ago. But this is just an opinion.

Configure category base to . (dot), put this code in the functions.php of your theme, or better in a plugin, and flush the rewrite rules (code from this post in Daily Web Kit):

add_filter( 'category_rewrite_rules', 'vipx_filter_category_rewrite_rules' );
function vipx_filter_category_rewrite_rules( $rules ) {
    $categories = get_categories( array( 'hide_empty' => false ) );

    if ( is_array( $categories ) && ! empty( $categories ) ) {
        $slugs = array();
        foreach ( $categories as $category ) {
            if ( is_object( $category ) && ! is_wp_error( $category ) ) {
                if ( 0 == $category->category_parent ) {
                    $slugs[] = $category->slug;
                } else {
                    $slugs[] = trim( get_category_parents( $category->term_id, false, '/', true ), '/' );
                }
            }
        }

        if ( ! empty( $slugs ) ) {
            $rules = array();

            foreach ( $slugs as $slug ) {
                $rules[ '(' . $slug . ')/feed/(feed|rdf|rss|rss2|atom)?/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
                $rules[ '(' . $slug . ')/(feed|rdf|rss|rss2|atom)/?$' ] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
                $rules[ '(' . $slug . ')(/page/(\d)+/?)?$' ] = 'index.php?category_name=$matches[1]&paged=$matches[3]';
            }
        }
    }
    return $rules;
}
  • I have done accordingly to your instructions. But, the "/page/2" is still not working. Is there any other solution to make it work? Thank you! – Jornes Dec 22 '14 at 8:47
  • 1
    I tested the code before post it and it works. Please, as it is said in the answer, remember to flush the rewrite rules (after copy and save the code, go to settings->permalinks and click on "save". This will make that WordPress rebuild the rewrite rules taking in account the new ones). – cybmeta Dec 22 '14 at 8:52
  • Hi! I tried. Your code for function is work. But, cannot put the "dot" into the category base, and i need to use the "301 Redirect" to redirect all the "domain.com/category/category-slug-name/" to "domain.com/category/category-slug-name/". Only the "/page/2/" work by then. – Jornes Dec 22 '14 at 10:00
  • I tested with the "dot" into the category base as you see in the answer, in fact that dot is needed. You didn't talk about any redirection and the way you have implemented the redirection is unknown to me, that is another issue that needs another thread. – cybmeta Dec 22 '14 at 10:04
  • 2
    There is a bug in this answer if pagination doesn't work for you - page 10 and more => fix: change this (\d)+ to this (\d+) and go to Permalinks setttings and re-save... (flush rewrite rules) – jave.web Mar 23 '17 at 16:59

This Worked for me, Add this function in your functions.php page in wordpress and this function will change your like:

Previous: http://www.example.com/advice/category/category_name/page/3/

To: http://www.example.com/advice/category_name/page/3/

function remove_category_slug_url($query_string){ 
    if ($query_string['name'] == 'page' && isset($query_string['page'])) {
        unset($query_string['name']);
        list($delim, $page_index) = split('/', $query_string['page']);
        $query_string['paged'] = $page_index;
    }      
    return $query_string;
}
add_filter('request', 'remove_category_slug_url');
function 
 remove_category_slug_url($query_string){ 
if ($query_string['name'] == 'page' && isset($query_string['page'])) {
    unset($query_string['name']);
    list($delim, $page_index) = split('/', $query_string['page']);
    $query_string['paged'] = $page_index;
}      
return $query_string;
 }
 add_filter('request', 'remove_category_slug_url');

This Worked for me, Add this function code in your functions.php in wordpress

note: go to settings->permalinks and click on "save". This will make that WordPress rebuild the rewrite rules taking in account the new ones

For anyone else having this problem, with the redirects on paginated pages, the following worked for me:

In your permalinks, change: /%category%/%postname%/

to

/%category%/%postname%

Notice the removal of the ending forward slash "/".

Rob

This worked for me.

function remove_page_from_query_string($query_string){ 
   if ($query_string['name'] == 'page' && isset($query_string['page'])) {
       unset($query_string['name']);
       // 'page' in the query_string looks like '/2', so i'm spliting it out
      list($delim, $page_index) = split('/', $query_string['page']);
      $query_string['paged'] = $page_index;
   }      
   return $query_string;
} 
add_filter('request', 'remove_page_from_query_string');

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.