4

I've created my own search functionality which basically finds the nearest store closest to the entered postal code.

My URL for the search currently looks like this http://www.example.com/stores?searchTerm=London, which is not really that SEO friendly. I would like my URL to be in the following format - http://www.example.com/stores/London, however due to my lack of knowledge in how the WordPress URL Rewrites work I'm struggling with this and would like some help in resolving this issue.

Stores is a page that loops through the results.

If anybody has any ideas on how to do this or can point me in the right direction then it would be greatly appreciated.

  • 3
    what is "stores"? is it a category, a tag, anything else? – JMau Jul 9 '13 at 22:17
1

You should add your own custom query variable first:

function add_search_store_query_var($vars) {
    $vars[] = 'search_store';
    return $vars;
}
add_filter( 'query_vars', 'add_search_store_query_var');

And then add rewrite rule:

function add_search_store_rewrite_rule() {
    add_rewrite_rule('stores/([^/]+)$', 'index.php?page_id=<YOUR SEARCH PAGE ID>&search_store=$matches[1]', 'top');
}
add_action('init', 'add_search_store_rewrite_rule');

You can then use get_query_var('search_store'); to get search term.

Just remember to flush rewrite rules, before you check it - it won't work without flushing rules. (Just go to permalink settings and click save).

PS. Coded it directly in here, so it can be a little bit buggy.

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.