Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Rewrite rules & pagination problem WordPress

I've added rewrite rules to my site to add /blog/ to the URL of all blog posts. But for some reason pagination isn't working. When I click through to page 2 or beyond I get a page not found error.

I have flushed the permalink cache by visiting & saving the permalinks page.
I'm using the wp pagenavi plugin, these are just regular posts not custom post types.

This is what I've added to functions.php



// add rewrite rules to add /blog/ to all single blog posts

add_filter( 'rewrite_rules_array', 'custom_rewrite_rule' );

function custom_rewrite_rule( $rules ) {
$new_rules = array(
'blog/([^/]+)/trackback/?$' => 'index.php?name=$matches[1]&tb=1',
'blog/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?name=$matches[1]&feed=$matches[2]',
'blog/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?name=$matches[1]&cpage=$matches[2]',
'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]'
);

$rules = $new_rules + $rules;

return $rules;
}


I'm thinking it's the final line that's causing the problem:
'blog/([^/]+)(/[0-9]+)?/?$' => 'index.php?name=$matches[1]&page=$matches[2]'

I tried changing &page to &paged, but no effect.

If any rewrite rule experts could help me out I'd appreciate it.


<strong>Update:</strong>

I need to add an extra clarification here - the reason I didn't use /blog/ in the permalinks page was because we need one blog category 'marketplace' to NOT have the /blog/ prepended in the URL.

So, if I add /blog/%postname%/ to the permalinks page all works as we'd like, except for the 'marketplace' category - so is there a rewrite rule I can use to REMOVE the /blog/ from posts in the 'marketplace' category?

Answers (4)

2016-02-09

Andrea P answers:

you don't need to add custom rewrite rules for the blog posts.

you can just change them from the permalink admin section.

use a custom structure and put:

/blog/%postname%/

this will prepend /blog/ to any blog post, but by instance it won't add it to pages


Andrea P comments:

that pagination error happens very often because you have another thing which ends up having the same permalink, and so when you load page/2/ it actually looks for another page..

this could happen by instance if you have a post type which has slug "blog", or has a post_type rewrite slug set as "blog".
so when you try to access this link
your-site/blog/page/2/
it's actually searching for a post called page/2/ within the post_type "blog"

it could also happen if you have a category with slug "blog" and in your settings the categories doesn't have a base slug (which is normally /category/ ). so when you try to access the above link, it is actually looking for the page 2 of the archive for the term "blog" of the category (or any other taxonomy which doesn't have a base-name, and has a term called "blog")

is your situation one of the above?


Mike Sewell comments:

It seems my original issue was the one you mentioned above where it wasn't finding page 2 because of the rewrite rules adding /blog/ into the URL. But I've switched over to your suggestion above adding /blog/%postname%/ to the permalinks screen and this works except for the issue I mentioned in my edit - is it possible to remove /blog/ from a single category using rewrite rules?

2016-02-09

Bob answers:

can you give us your website url?

2016-02-09

Arnav Joy answers:

yes please show us url

2016-02-10

Rempty answers:

Hello
If you want a pagination like /blog/pagename/page/2
add this rewrite rule

'blog/([^/]+)/page/?$' => 'index.php?name=$matches[1]&paged=$matches[2]'