Rewrite wordpress url

I have a url like this

xxxxx.com/?s=&cp_state=Porto&refine_search=yes

Read More

and I try to make a url like this

xxxxx.com/Porto

Already tried to use this code:

    function search_url_rewrite_rule() {
    if ( is_search() && !empty($_GET['s'])) {
        wp_redirect(home_url("/search/") . urlencode(get_query_var('s')));
        exit();
    }   
}
add_action('template_redirect', 'search_url_rewrite_rule');

But this code gives me a url like this

xxxxx.com/search/

Could you help me please?

Related posts

Leave a Reply

1 comment

  1. I can’t swear this will work but you’re looking for something like this:

    add_rewrite_rule('([^/]*)/?','index.php?s=&cpstate=$matches[1]&refine_search=yes','top');
    

    I doubt the wisdom of rewriting based on the first segment in the URL, in case you want to switch over to pretty links for the rest of the site later on you will get a lot of odd results. For example example.com/blog would search for blog, example.com/search for search for search and so on… if you want something else you can just edit the regexp so that it fits what you want.

    Read more here: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule