0

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.

0

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.

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.