Wordpress Customer URL Rewrite broke after udpate

Viewed 120

I had a simple rewrite that seems to be broken after a WP update.

In my functions.php I have:

function custom_rewrite_basic() {
    add_rewrite_rule('^design/*', '/index.php?page_id=24565', 'top');
}
add_action('init', 'custom_rewrite_basic');

And in my htaccess I have:

RewriteRule ^^design/* //index.php?page_id=24565 [QSA,L]

The goal is to have an end-user load domain.com/design/123 and then the design page loads which references page id 24565 but after the page loads the 123 ID should still be attached to the URL.

Currently, if I try to load domain.com/design/123 it loads doamin.com/design and drops the 123 ID. Any suggestions? I recently updated WP to latest version and I think it broke it.

1 Answers

First off, you don't need that RewriteRule in your .htaccess file, so just remove that line.

And the issue with the 123 being "removed" from the URL is because that part is seen by WordPress as the page number (for paginated requests via the <!--nextpage--> tag or the "Page Break" block in the Gutenberg block editor), and when the page number is greater than the max number of pages, then WordPress redirects you to page #1 of that page (instead of showing a 404 error page).

So to fix the issue, just append &page= (or &page=1 would also work) to the rewrite rule's query, which means we're setting the page number to 1 at all times:

add_rewrite_rule( '^design(?:/\d+|)/?$', 'index.php?page_id=24565&page=', 'top' );

And this isn't required, but I changed the RegEx pattern (the first parameter above) so that it matches example.com/design and example.com/design/<number> only. If you don't like that, then just use your existing RegEx pattern (^design/*).

So I hope that helps, and remember to flush the rewrite rules — just visit the permalink settings admin page, without having to make any changes.

Related