WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am facing problem to redirect some url in wordpress. I need to rewrite following URL:

First URL is like this: http://example.com/news/?news=2 where news=2 is the news id and I am getting this id by using $_GET['news']. I want to rewrite this above url to http://example.com/news/2/

Second URL is like this: example[dot]com/product-list/?pp=2&page=3/ and I want to rewrite it as example[dot]com/product-list/2/3/

Thanks in advance for help.

share|improve this question

For this kind of rewriting, I'm using this code (haven't tested this one exactly, so may contain minor bugs):

add_filter( 'query_vars', 'binda_query_vars' );
function binda_query_vars( $vars ) {
    $vars[] = 'pp';
    return $vars;

add_action( 'generate_rewrite_rules', 'binda_rewrite_rules' );
function binda_rewrite_rules( $wp_rewrite )
    $wp_rewrite->rules = array(
        'news/([0-9]{1,})/([0-9]{1,})/?$' => $wp_rewrite->index . '?post_type=news' . '&pp=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 2 ),
        'news/([0-9]{1,})/?$' => $wp_rewrite->index . '?post_type=news' . '&pp=' . $wp_rewrite->preg_index( 1 ),
        'product-list/([0-9]{1,})/([0-9]{1,})/?$' => $wp_rewrite->index . '?post_type=product' . '&pp=' . $wp_rewrite->preg_index( 1 ) . '&paged=' . $wp_rewrite->preg_index( 2 ),
        'product-list/([0-9]{1,})/?$' => $wp_rewrite->index . '?post_type=product' . '&pp=' . $wp_rewrite->preg_index( 1 ),
    ) + $wp_rewrite->rules;

You may need to replace post_type=news and post_type=product for appropriate post_type created in your system.

Note that this does not replace your permalinks in a template, you have to do that manually.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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