Change default search URL slug in WordPress

Unless your website is a one-pager which only contains the name of your company and contact info, if you ask us, a search feature is a must-have feature. It doesn’t matter if you are just starting a blog or already have thousands of posts and pages on your website, whether you write as a hobby or make money from your blog, you have to do everything to keep your visitors on the site. Giving them an easy and quick way of searching through your website is definitely one of the crucial things you should take care of.

It doesn’t matter if you use WordPress’ built-in search engine or if you have installed a different one, you need to take care of search results.

We already showed you how to exclude posts and pages from search results and how to redirect a user directly to the post if there was only one search result found. While there are many things you can do about your search feature, you may have forgotten about one simple yet important thing. Yes, we’re talking about that URL slug which appears in the address bar once search returned the result page.

By default, WordPress uses a non-user-friendly version of the slug. Not only your visitor will find that slug weird, but it may harm your SEO as well. Better to say, you can help your SEO by changing the slug into something understandable both to humans and machines.

If you have tried searching through your website, you could have seen that strange /?s=term slug which appears in the address bar. Today, we’re about to show you how to change that into anything you want. For example, we’re about to change “/?s=term” into “/search/term” which will be easier to remember and use.

  1. Open functions.php
  2. Copy and paste the following:
  3. function feed_dir_rewrite( $wp_rewrite ) {
        $feed_rules = array( 'search/(.+)' => 'index.php?s=' . $wp_rewrite->preg_index(1));
        $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
    }
    add_filter( 'generate_rewrite_rules', 'feed_dir_rewrite' );
    
  4. Change destination URL on the third line to whatever you like
  5. Save changes

After you’ve done these changes to your functions.php file, you can try out your new URL slug. Don’t worry; if a plugin or a user tried to get access to your search result by navigating to old URL slug, your website won’t return an error but simply redirect the search result to the new URL.

If you want to change the slug to something unique, you can do that on the third line of the code.

This one will take your search results page into www.yourdomain.com/search/search-term

Now you can relax and stop thinking about how search results are displayed to your users as well as to search engine bots who will now more easily crawl through those results. Also, isn’t it somehow more natural to see the whole “search” word instead of that weird question mark followed by letter “s”? Tell us what you think.

 

START YOUR OWN BLOG

This guide is an introduction to mastering the art of blogging. It provides easy to follow steps to start, maintain, and grow your blog.

Read the guide

3 thoughts on “Change default search URL slug in WordPress

  1. Hi,
    I’ve done that but it works only with /search/. If I try to change /search/ with other, I get 404 error page.
    Here is what I wrote on function.php:

    function search_url_rewrite () {
    if (is_search() && !empty($_GET[‘s’])) {
    wp_redirect(home_url(‘/anything/’).urlencode(get_query_var(‘s’)));
    exit();
    }
    }
    add_action(‘template_redirect’,’search_url_rewrite’);

    • Hi,
      Thank you for pointing that out!

      I have just updated the code in the article; please try using this one to change the search URL.

      Also, there is a free WP Search URL plugin that you can try.

Leave a Reply

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