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.

Published by Dragos Micu

WooCommerce

Leave a Reply

Your email address will not be published. Required fields are marked *

what does keto blast gummies do unlock the power of a keto friendly weight loss solution unlock the power of dr juan rivera gummies keto the ultimate solution for a low carb lifestyle is there a cbd gummy for weight loss unlocking the science behind cbd gummies for a healthier you do any keto gummies really work top picks for a successful keto journey experience the power of keto gummies with apple cider vinegar a comprehensive guide to achieving su what are kelly clarksons weight loss diet gummies all about experience the ultimate relaxation with gummies cbd melatonine a comprehensive guide to unlocking b the ultimate guide to buy cbd gummies for a restful night 39 s sleep and relaxation experience the perfect balance cbd thc hybrid gummies for relief and relaxation exploring the cbd gummies pro and con a comprehensive guide to making an informed decision 76869 experience the ultimate relaxation with gummies cbd delta 9 a comprehensive guide to cbd edibles fo can cbd gummies show up on a drug test what you need to know about cbd detection do cbd thc gummies help with sleep unlocking the secret to a restful night 39 s rest how long does a 20mg cbd gummy last unlocking the science behind cbd edibles enhancing confidence with male butt enhancing underwear a comprehensive guide 99134 kangarise male enhancement reviews boosting your ingredients in the bedroom discover the power of cbd gummies fort wayne unlocking a healthier you with malebiotix cbd gummies erectile dysfunction pills online a comprehensive guide to effective treatment 770 pure harmony cbd male enhancement gummies a natural solution for ed treatment cbd gummies for mal access to free erectile dysfunction medication a comprehensive guide to treatment options 97230