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.
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
Is there some SEO advantage by using this instead of the default URL?
Nice function BTW, i’ll use it.
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= .