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 *

say goodbye to diabetes symptoms with pure canna cbd gummies a natural and effective solution discover the convenience of cbd gummies near by me a comprehensive guide to choosing the best produ the best cbd gummy recipe a comprehensive guide to creating delicious and potent cbd gummies at hom understanding cbd gummies how much cbd is in a gummy experience the ultimate relaxation with terra bliss cbd gummies a game changer for seniors and beyo managing diabetes with bioblend cbd gummies a comprehensive guide 519 gummy bears that help you sleep a comprehensive guide to better rest 6162 finding the best cbd gummy for sleep a comprehensive guide bioheal cbd gummies official website improve health amp helps in pain relief will cbd gummies make you fail a drug test separating fact from fiction 16972 experience the ultimate relaxation with cbd fx gummies a comprehensive review snooze cbd gummies the ultimate sleep aid and relaxation solution for a happier you revitalize your mind and body with renew calm cbd gummies amazon a comprehensive review of the top discover the soothing power of sera relief cbd gummies unlocking a healthier you will cbd gummies show up on a drug test separating fact from fiction and choosing the best cbd gumm cbd gummies for relaxation a comprehensive guide to reducing anxiety and stress with highline welln 162 are cbd gummies legal in kentucky a comprehensive guide to prime cbd gummies harmony leaf cbd gumm will cbd gummies fail a drug test a comprehensive guide dr oz cbd gummies the ultimate solution for anxiety pain and overall wellness revolutionizing the cannabis industry the importance of effective cannabis gummy packaging