WordPress rewrite rule for pretty URL not working

936 views Asked by At

I am using Wordpress rewrite rules and I want to have a URL like this:

http://my-app.com/data-library/brand_name

Here is my code:

function my_add_rewrite_rules() {
    add_rewrite_rule(
        '^data-library/([^/]*)/?',
        'index.php?pagename=data-library&brand_name=$matches[1]'
    );
    //flush_rewrite_rules();    
}
add_action('init', 'my_add_rewrite_rules')

I also use flush rewrite rules and save permalink but still a 404 Page not found error

Need help

Thanks

1

There are 1 answers

0
Shashank Sharma On

You need to apply with this modified code

function my_add_rewrite_tags() {
      add_rewrite_tag('%brand_name%', '([^&]+)');
}
add_action('init', 'my_add_rewrite_tags', 10, 0);

function my_add_rewrite_rules() {
    add_rewrite_rule(
        '^data-library/([\w+]*)/', 'index.php?pagename=data-library&brand_name=$matches[1]', 'top');
    );
    //flush_rewrite_rules();    
}
add_action('init', 'my_add_rewrite_rules');

Please reset permalinks because htaccess file updated after saving permalinks