2

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

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' );
6
  • 2
    This should be handled with WordPress rewrite rules system, not htaccess. see the answer to this question.
    – Milo
    Feb 3 '12 at 17:58
  • Could you rephrase? It's not clear what directory/view is, how it's being generated, and what is generating it. Is it a taxonomy? Custom plugin? There's not enough information to answer this question.
    – Tom J Nowell
    Feb 3 '12 at 17:58
  • @Tom J Nowell, I creat an empty page directory then creat an page view which pareant page is directory, all this finish in admin panel add page. but url rewrite rule not suit for it... Thanks.
    – yuli chika
    Feb 3 '12 at 18:42
  • @Milo, I tried it by myself, but still not work...
    – yuli chika
    Feb 3 '12 at 18:48
  • @yulichika - all requests in WordPress get routed to index.php, your rewrite rule should point to index.php with pagename= set to the page you want to point it to, directory/view in this case.
    – Milo
    Feb 3 '12 at 18:53
3

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.

11
  • still some trouble, I checked the .htaccess something added: RewriteRule ^^directory/view/(.+)/(\d+)/ /wp/?pagename=directory/view?search=$matches[1]&number=$matches[2] [QSA,L] .by the way, my rewritebase is RewriteBase /wp/
    – yuli chika
    Feb 3 '12 at 19:06
  • forgot the index.php in the rewriterule. See answer now. Btw I don't know if /wp mathers, but if this won't work change ('^directory/view/ in the rewriterule to '^wp/directory/view/ and try again. Feb 3 '12 at 19:13
  • @this time show 404, If I remove RewriteRule, open http://localhost/wp/directory/view?search=moto&number=12, I can visit the page. now the .htaccess is RewriteRule ^^directory/view/(.+)/(\d+)/ /wp/index.php?pagename=directory/view?search=$matches[1]&number=$matches[2] [QSA,L]
    – yuli chika
    Feb 3 '12 at 19:23
  • Edited my answer tested in on my own site with "/blog/directory/view?search=test&number=2" Feb 3 '12 at 19:41
  • +1 for your kindly, but still 404 in mine local test. And also test in your page http://vermeertech.nl/blog/directory/view?search=test&number=2.
    – yuli chika
    Feb 3 '12 at 20:05

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.