0

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?

1

I figured it out. I put these rewrite rules in .htaccess:

RewriteCond %{QUERY_STRING} ^id=([^/]*)&n=([^/]*)$ 
RewriteRule ^show/?$ /show\/%1\/%2\/? [R=301,L]
  • You should mark your answer as correct – Tunji Jan 6 '17 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.