I’ll add another answer because the rule is only a part of the problem.
Here is what I have tested and is working :
<?php
class BadRewritesRules {
public function __construct() {
add_action('init', [$this, 'custom_post_types_rewrite_rules']);
remove_filter('template_redirect', 'redirect_canonical');
}
public function custom_post_types_rewrite_rules() {
add_rewrite_rule('(blog)/([0-9-]+)[/]?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
}
}
new BadRewritesRules();
In reality, I used another URL and you can see it working here : https://tests.pierre-roels.com/bad-rewrite-rules/2/
You can use pagination and also view each “member” with the same slug.
But as I said, this is a poor solution in my opinion because we must remove the template_redirect
action hook. It would be better to hook on the Laravel part to build links the right way 🙁 Or maybe an .htaccess
redirect ?
Don’t forget to flush permalinks everytime you make a change. To do so, you can simply go to Settings > Permalinks and submit the page
Here is the solution with .htacess. Add this to your .htaccess :
RewriteRule (blog)/([0-9]+)/? /$1/page/$2/ [R=301,L]
and don’t forget to fix you rule :
add_rewrite_rule('(blog)/page/([0-9-]+)[/]?$', 'index.php?pagename=$matches[1]&paged=$matches[2]', 'top');
.
I have made another test just for you : https://tests.pierre-roels.com/blog/2/
This solution allow you to not remove the template_redirect
hook, it redirects the user the right end-url