2

I'm trying to get http://example.com/site/vendors/?u=abc to rewrite to http://example.com/site/vendors/abc but I can't for the life of me figure out how to get the url rewrites to work.

/site/ is the root directory for my WP installation /vendors/ is a page

2 Answers 2

3

assuming u is a custom query var, you have to first add it to the array of recognized query vars:

function wpa_query_vars( $query_vars ){
    $query_vars[] = 'u';
    return $query_vars;
}
add_filter('query_vars', 'wpa_query_vars');

Then add an internal rewrite rule that accepts anything appended to vendors and passes that as the u query var:

function wpa_rewrite(){
    add_rewrite_rule(
        'vendors/([^/]+)/?$',
        'index.php?pagename=vendors&u=$matches[1]',
        'top'
    );
}
add_action( 'init', 'wpa_rewrite' );

Make sure to flush rewrite rules once for this rule to be added, you can also do this by just visiting the Settings > Permalinks admin page.

Then in the template, you can access the value of u via get_query_var():

$vendor = get_query_var( 'u' );
1

I believe this is what you are looking for:

RewriteRule ^/site/vendors/([^/]*)$ /site/vendors/?u=$1 [L]
2
  • I'm afraid he was asking about doing this with WP_Rewrite, not with mod_rewrite in .htaccess ;) Commented Jul 3, 2013 at 18:30
  • Great attempt David, I thank you for spending the time helping me Commented Jul 3, 2013 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.