unexpected problem in url rewrite

As per the answer provided in How to create custom URL routes? I have created the rewrite rule using the same function provided.

add_action( 'init', 'wpse26388_rewrites_init' );
function wpse26388_rewrites_init(){
    add_rewrite_rule(
        'gallery/([0-9]+)/?$',
        'gallery?custom_gallery_id=$1',
        'top' );        
}

After the flushing of the rules .htaccess file was having the following rule :

Read More
RewriteRule ^gallery/([^/]+)/?$ /wordpress/gallery?custom_gallery_id=$1 [QSA,L]

upto this point everything was fine then I tried to make some modifications in the rule like

add_action( 'init', 'wpse26388_rewrites_init' );
    function wpse26388_rewrites_init(){
        add_rewrite_rule(
            'topic/([^/]+)/([^/]+)/gallery/([0-9]+)/?$',
            'gallery?custom_gallery_id=$3',
            'top' );        
    }

this doesn’t seems to be working at all. can anybody help me over this?
thanks in advance.

Related posts

Leave a Reply

1 comment

  1. first, you need to add your custom_gallery_id to query vars if you haven’t already:

    add_filter( 'query_vars', 'wpse26388_query_vars' );
    function wpse26388_query_vars( $query_vars ){
        $query_vars[] = 'custom_gallery_id';
        return $query_vars;
    }
    

    for your rewrite rule, you need to load a WordPress object– a page, a category, etc., and all internal rewrites must point to index.php. if topic, place, and new delhi are all pages, it would look something like this:

    function wpse26388_rewrites_init(){
        add_rewrite_rule(
            'topic/([^/]+)/([^/]+)/gallery/([0-9]+)/?$',
            'index.php?pagename=topic/$matches[1]/$matches[2]&custom_gallery_id=$matches[3]',
            'top' );        
    }
    

    also try out this plugin for analyzing and testing your rewrite rules.