How to make search URL prettier in WordPress

In this guide, I will should you a simple trick to make your website’s search results page URL much nicer.

With WordPress, you can make your posts URLs prettier from within Settings -> Permalinks.

Unfortunately there isn’t an option to improve the look of your search results page, and by default looks like this:

www.example.com/?s=your-query

As you can see, this doesn’t look too nice. I personally believe the search results page URL should be prettified by default in WordPress, and if you don’t yet know what I mean by “prettifying” it, I mean that the search results page should look like this:

www.example.com/search/your-query

Method 1 – Using a WordPress action

The first method requires you to add the following snippet into the functions.php file of your child theme. It taps into the template_redirect action, and checks if it’s the search page and that the search “s” parameter is not empty. If everything checks out, it redirects you to the prettier version of the results page.

// https://headless.wpharvest.com/how-to-make-search-url-prettier-in-wordpress/ function wpharvest_change_search_url() { if ( is_search() && ! empty( $_GET['s'] ) ) { wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) ); exit(); } } add_action( 'template_redirect', 'wpharvest_change_search_url' );
Code language: PHP (php)

Method 2 – Modifying the htaccess file

If you have access to the server and prefer to modify the .htaccess file instead, you can add a rewrite conditions as such:

# Prettify the search URL RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC] RewriteRule ^$ /search/%1/? [NC,R,L]
Code language: PHP (php)

I personally believe this should be one of those scripts that you always add to all your websites. If you found this useful or are interested in more “must have” scripts, feel free to leave a comment below.


The go-to automatic tax calculations service for your WooCommerce site.