Rewrite Search Result URL for WordPress

If you have ever done a search on a WordPress site you have probably noticed that the URL ends up looking something like this:

http://your-site.com/?s=searchterm

Even if you have set up your site to display pretty permalinks you will still get that “s” parameter in your URL. For some reason, WordPress does not rewrite the search results URL even though a “pretty” search result URL exists somewhere in the core functions.
Try typing this into your site (remember to replace your-site.com with your URL):

http://your-site.com/search/the

You will get a search results page with actual results for the search term “the.” So the function exists but the rewrite rule does not. You will need to add this function to your functions.php file to put a rewrite rule in place:

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

This function will now rewrite every search results URL to something like this:

http://your-site.com/search/searchterm

I have just implemented this technique on bavotasan.com so do a search to test it out.

Share this:

Email
Facebook
Twitter
Pinterest
Pocket

Premium Themes for WordPress

Looking for an easy-to-use Premium Theme for WordPress? Check out Themes by bavotasan.com and have your site up and running in no time.

Use this WordPress website builder to build powerful websites in no time for your or your clients.

WordPress Hosting

WP Engine – designed from the ground-up to support and enhance the workflow of web designers.

Bluehost – providing quality web hosting solutions since 1996.

About the author

Picture of Luke Perrie

Luke Perrie