I have been working on a WordPress website where all blog posts are shown on a custom page called /blog/ using a custom page template with a custom query.
With that in mind, I needed to modify the default post URL to include an additional slug in the URL. E.g. "example.com/post-name/" would become "example.com/blog/post-name/".
Doing it via the standard "permalinks" page was not an option for this particular website, so I did it via custom rewrite rule in my theme function.php file.
This worked fine, but it broke pagination on my /blog/ page. The page works fine, but the pagination on the page does not work with this rewrite rule in place, so when going to "example.com/blog/page/2/" I get a 404 error.
I figured out that I need to update my rewrite rule to EXCLUDE instances where the URL is "/blog/page/x/" because at the moment, my re-write rule is being applied to anything that is "/blog/x".
That being said, I cannot figure out how to modify my re-write rule as needed. I tried:
I thought that if I defined a second rule that applies to instances where the incoming url is "/blog/page/xxx" then it would override the original rule and allow everything to work properly, but it didn't work out this way.
Would appreciate if someone could help me figure this out!
<strong>EDIT 1:</strong>
Tried to implement a solution found on <a href="https://stackoverflow.com/questions/43044611/wordpress-rewrite-rule-to-exclude-specific-slug">WordPress Rewrite Rule to exclude specific slug</a>
This did not work. Perhaps I implemented it in the wrong place....not sure.
With that in mind, I needed to modify the default post URL to include an additional slug in the URL. E.g. "example.com/post-name/" would become "example.com/blog/post-name/".
Doing it via the standard "permalinks" page was not an option for this particular website, so I did it via custom rewrite rule in my theme function.php file.
Code:
function add_rewrite_rules( $wp_rewrite ){
$new_rules = array(
'blog/(.+?)/?$' => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
This worked fine, but it broke pagination on my /blog/ page. The page works fine, but the pagination on the page does not work with this rewrite rule in place, so when going to "example.com/blog/page/2/" I get a 404 error.
I figured out that I need to update my rewrite rule to EXCLUDE instances where the URL is "/blog/page/x/" because at the moment, my re-write rule is being applied to anything that is "/blog/x".
That being said, I cannot figure out how to modify my re-write rule as needed. I tried:
Code:
function add_rewrite_rules( $wp_rewrite ){
$new_rules = array(
'blog/(.+?)/?$' => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1),
'blog/page/([^/]+)/?' => 'index.php?pagename=blog&paged=$matches[1]',
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
I thought that if I defined a second rule that applies to instances where the incoming url is "/blog/page/xxx" then it would override the original rule and allow everything to work properly, but it didn't work out this way.
Would appreciate if someone could help me figure this out!
<strong>EDIT 1:</strong>
Tried to implement a solution found on <a href="https://stackoverflow.com/questions/43044611/wordpress-rewrite-rule-to-exclude-specific-slug">WordPress Rewrite Rule to exclude specific slug</a>
Code:
function add_rewrite_rules( $wp_rewrite )
{
$new_rules = array(
'blog/(?!page)(.+?)/?$' => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1),
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'add_rewrite_rules');
This did not work. Perhaps I implemented it in the wrong place....not sure.