I have a custom page like this:
website.com/show/?id=9999&n=show-name
/show/
is a WordPress page with a postid of 540. I added a rewrite rule to convert the URL to:
website.com/show/9999/show-name/
Here's the code I'm using in my theme's functions.php
for the rewrite:
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^show/([^/]*)/([^/]*)/?',
'index.php?page_id=540&id=$matches[1]&n=$matches[2]',
'top' );
}
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'id';
$vars[] = 'n';
return $vars;
}
Now the URL website.com/show/9999/show-name/
works correctly.
However, now I need to make sure that if someone tries to access the page using the old URL:
website.com/show/?id=9999&n=show-name
... that they will be redirected to the new SEO friendly url:
website.com/show/9999/show-name
How do I setup that redirect?