X

How to Dynamically Customize WordPress Search URL

Snippets by IsItWP

Want to customize the WordPress search URL of your site? You can dynamically create a custom-tailored URL based on the actual search term your visitors type in your search bar.

With the below snippet, this is how your search URL will look like:
http://example.com/search/searchterm

Instructions:

All you have to do is add this code to your theme’s functions.php file or in a site-specific plugin:

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');

Note: If this is your first time adding code snippets in WordPress, then please refer to our guide on how to properly copy / paste code snippets in WordPress, so you don’t accidentally break your site.

If you liked this code snippet, please consider checking out how to connect Google Analytics to your site, so you can track your on-site search with ease.

Comments   Leave a Reply

  1. Luis Sacristán June 6, 2012 at 3:57 pm

    Thanks for the snipp, but there is a problem when you try to search words with accents (á, é, í, ó, ú) or others letters (ñ, ç, …)

    You have to decode the search term in the query_vars and in the search_query. Here is my code

    function you_search_filter($s) {
    return urldecode($s);
    }

    add_filter(‘get_search_query’, ‘you_search_filter’);

    add_filter(‘the_search_query’, ‘you_search_filter’);

    add_action(‘parse_query’, ‘you_query_vars_search_filter’ );

    function you_query_vars_search_filter( $qvars ) {
    $qvars->query_vars[‘s’] = urldecode($qvars->query_vars[‘s’]);
    return $qvars;

    }

    Hope it could be useful

  2. Is there some SEO advantage by using this instead of the default URL?
    Nice function BTW, i’ll use it.

    1. To be honest I don’t think it is that big of a help to SEO however if you have pretty links enabled this continues this look rather then ?s= .

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!