custom url rewrite

I have a custom search, one database out of WP, I store the page in wordpress admin panel:

http://localhost/wp/wp-admin/post.php?post=96&action=edit&message=1

Read More

Then the page’s url looks like http://localhost/wp/directory/view?search=moto&number=12

Now I want to modify it into http://localhost/wp/directory/view/moto/12. I tried the method with RewriteRule ^directory/view/(.*)/(d+)/$ directory/view?search=$2&number=$3, and the whole .htaccess is:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp/
RewriteRule ^index.php$ - [L]
RewriteRule ^directory/view/(.*)/(d+)/$ directory/view?search=$2&number=$3 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
# END WordPress

but it does not work, need a work code, thanks,

EDIT:
I tried add_rewrite_rule still not worked… To avoid page name infection, the better way use directory/view.

function add_directory_rewrite() {
    add_rewrite_tag("%search%", '(.+)');
    add_rewrite_tag("%number%", '(d+)');
    add_rewrite_rule('^directory/view/(.+)/(d+)/', 'directory/view?search=$matches[1]&number=$matches[2]', 'top');
}
add_action( 'init', 'add_directory_rewrite' );

Related posts

Leave a Reply

1 comment

  1. Your rewriterule should be:

    add_rewrite_rule('^directory/view/(.+)/(d+)/', 'index.php?pagename=directory/view&search=$matches[1]&number=$matches[2]', 'top');
    

    Remember to flush your rewriterules after every edit (go to settings > permalinks).


    EDIT added complete code example.

    function add_directory_rewrite() {
        add_rewrite_tag("%search%", '(.+)');
        add_rewrite_tag("%number%", '(d+)');
        add_rewrite_rule('^directory/view/(.+)/(d+)', 'index.php?pagename=directory/view&search=$matches[1]&number=$matches[2]', 'top');
    }
    add_action( 'init', 'add_directory_rewrite' );
    

    In page.php for testing purposes:

    <?php echo get_query_var("search"); echo get_query_var("number"); ?>
    

    This worked on my own site, even changed the blog url to /blog/ to test the structure.

    If this still doesn’t work, maybe empty your htaccess an save it in settings > permalinks again (make sure the file is writable).

    Removed trailing slash from '^directory/view/(.+)/(d+)/' and it works.