SearchWP Blog

WordPress Tutorials, Tips, and Resources to Help Grow Your Business

How To Change The Default Search URL In WordPress - thumbnail

Do you want to change the default search URL in WordPress?

When users perform a search, WordPress generates a URL that looks something like this: http://example.com/?s=search-term.

Many website owners prefer to change the format as it is hard to read.

You can improve the readability of your website’s search URL by changing the default slug to something cleaner, like http://example.com/search/search-term.

Therefore, in this article, we’ll show you how to change the default search URL in WordPress safely.

Key Takeaways:

  • The default WordPress search URL is difficult to read and not user friendly.
  • Having a cleaner URL helps search engines better understand your site and rank your content appropriately.
  • You can safely change the default search URL slug without editing core files using WPCode.

Why Change The Default Search URL In WordPress?

By default, WordPress uses a user-friendly URL structure for most pages and posts. For example, pages may look like this:

  • http://example.com/post-url/
  • http://example.com/2018/03/post-url/
  • http://example.com/category/post-url/

These URLs are clean, easy to understand, and provide useful information to both your site visitors and search engines.

However, when a user performs a search on your website, WordPress provides the result on a page with a complex URL format:

  • http://example.com/?s=search-term.

As you can see, the “?s=” in the URL may confuse both users and search engines. Here are a few reasons why changing this default search URL slug can be beneficial:

  1. SEO Benefits: According to some experts, search engines like Google prefer clean URLs because they provide better context for a page. For example, a URL like http://example.com/search/shoes is more SEO-friendly than http://example.com/?s=shoes. A clean search slug helps search engines index and rank your pages more effectively.
  2. Improved User Experience: Clear, readable URLs are easier for users to remember and navigate. A URL like http://example.com/search/best-laptops is more professional and user-friendly than the default ?s=best-laptops.
  3. Brand Consistency: Clean URLs keep your brand image polished. Untidy URLs can diminish the professionalism of your site, while a clean search slug reinforces your brand’s quality and trustworthiness.

For instance, imagine a customer is searching for “smartphones” on an online store, if the search results page URL is http://example.com/search/smartphones, it becomes easy for them to remember and share the URL with friends and family.

How To Change The Default Search URL In WordPress

To change the default search URL in WordPress, we need to add custom code to your website’s codebase. There are different ways to do that.

You can add code to your website by using a separate plugin or FTP. While one is safer than the other, we’ll show you both in this tutorial. However, please follow one of the following methods.

The easiest and safest way to add code to your site is by using custom code snippets.

Code snippets are short, reusable pieces of code that you can add to your website to customize its functionality or introduce new features, all without modifying your theme, plugins, or core WordPress files.

Now, the best way to add code snippets to your website is to use WPCode. It’s the most popular code management plugin for WordPress. It inserts code into your website without modifying the actual codebase.

How To Change The Default Search URL In WordPress: WPCode Plugin in WordPress plugin repository

Using WPCode, anyone, from beginners to experts, can easily add, edit, and manage code snippets to WordPress with its user-friendly interface. Not to mention, it’s completely free!

Here are a few key benefits of using WPCode:

  • Curated Snippet Library: WPCode provides a vast library of pre-approved snippets, carefully vetted by the WPCode team, so you can easily enhance your site’s functionality without needing additional plugins.
  • Custom Insertion Options: WPCode gives you full control over snippet placement. You can choose exactly where the code appears, whether after a certain number of words in a post or on a specific page.
  • Advanced Conditional Logic: WPCode’s smart conditional logic allows you to set precise rules for when and where snippets should be applied based on factors like user roles, post types, or taxonomy terms.
  • Supports Multiple Code Types: Whether you need to add HTML, PHP, JavaScript, CSS, or even text snippets, WPCode has you covered. Snippets can be inserted via shortcodes or automatically.
  • Built-in Error Handling: If an issue arises, WPCode’s safe mode lets you access your site for quick fixes. Error logs are available to help you diagnose and resolve any problems.

To add code snippets to your site that’ll change the default search URL in WordPress, please install WPCodeYou can set it up like any other WordPress plugin.

If your installation is successful, you’ll see a new menu item called Code Snippets appear in the left sidebar. Now, please navigate to Code Snippets > Add New.

How To Change The Default Search URL In WordPress: Adding code snippet step 1

On this page, you’ll see various code snippets. Each of these can be used to add a particular feature to your website. Since we’ll be adding custom code, please hover over the “Add Your Custom Code” card and click on the “Add Custom Snippet” button.

How To Change The Default Search URL In WordPress: Adding code snippet step 2

Now, give your snippet a proper name, and choose the Code Type as “PHP Snippet”. Then, paste the following code. You can change the keyword from “/search/” to anything else you prefer.

function custom_search_url_rewrite() {
    if ( is_search() && ! empty( $_GET['s'] ) ) {
        wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
        exit();
    }
}
add_action( 'template_redirect', 'custom_search_url_rewrite' );

Since we’ll only change the visitor-facing URL, let’s load this snippet to only frontend. To do that, from the Insertion section, choose “Auto Insert” as the Insert Method and “Frontend” as the Location.

How To Change The Default Search URL In WordPress: Adding code snippet step 3

We’re almost set. Please activate the snippet by turning on the following toggle and clicking “Save Snippet” to put it into action.

How To Change The Default Search URL In WordPress: Adding code snippet step 4

That’s it. You have successfully added the code snippets to change the default search URL in WordPress.

Method 2: How To Change The Default Search URL In WordPress Using FTP

Another way to change the default search URL in WordPress is to edit your site’s .htaccess file and add the code responsible for changing the URL.

However, this approach is more complicated and not advised for beginners. We recommend only advanced users who have worked with FTP before follow this route. This method gives you greater control and does not require additional plugins.

To add code to your .htaccess file via FTP, you’ll need an FTP client like FileZilla to connect to your website’s server. Alternatively, you can use other clients like WinSCPCyberduck, or Transmit, depending on your preference.

Use the credentials provided by your hosting provider to access your server.

How To Change The Default Search URL In WordPress: Adding code using FTP step 1

Once connected to your server, locate and open the public_html folder, where you’ll find the .htaccess file.

How To Change The Default Search URL In WordPress: Adding code using FTP step 2

Now, download the .htaccess file to your computer. Before making any changes, it’s a good idea to keep a backup of the original file in case anything goes wrong; you can use it to undo mistakes.

Next, open the .htaccess file in a text editor such as Notepad++ or Sublime Text, and insert the following code at the bottom of the file:

# Change the default WordPress search results page URL

RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]

Here’s what it looks like:

How To Change The Default Search URL In WordPress: Adding code using FTP step 3

After adding your code, save the file and upload it back to the public_html folder via FTP, replacing the current .htaccess file. In FileZilla, you can do this by simply dragging and dropping the updated file into the window.

How To Change The Default Search URL In WordPress: Adding code using FTP step 4

That’s it! You’ve successfully modified your .htaccess file to change the default WordPress search URL.

See The Changed URL In Action

Now that you have successfully added the code to change the default search URL for your WordPress site, it’s time to see if everything’s working as expected. To do that, please visit the frontend of your site and perform a search. You should see the following:

How To Change The Default Search URL In WordPress: Changed URL in action.

What’s Next?

Now that you have a cleaner search URL slug, it’s time to think about enhancing your search functionality even further. The default WordPress search is functional, but it’s fairly limited.

You can significantly improve your website search by using SearchWP. It’s the most powerful WordPress search plugin that allows you to customize your site’s search engine, helping users find the most relevant content faster. Some standout features of SearchWP include:

  • Custom Search Algorithms: You can create tailored search rules to promote specific types of content (like posts, pages, or products) that matter most to your audience.
  • Keyword Stemming: Improve search results by including results for related terms. For example, searching for “run” can show results for “running” or “runner” as well.
  • Search by Category or Tag: This feature allows users to narrow their search results by category or tag, making it easier for them to find what they’re looking for.
  • Boost Product Search for WooCommerce: If you run an eCommerce site, SearchWP can dramatically improve your product search, helping customers find exactly what they need with fewer clicks.

Conclusion

We hope you were able to change the default search URL in WordPress following this tutorial. This simple but powerful tweak improves your site’s user-friendliness and significantly boosts SEO.

And, if you want to improve your site’s search functionality, get SearchWP for your website to significantly enhance user experience and boost your business!

author avatar
Saif Khan

A Better WordPress Search Experience

Get the best WordPress search plugin. Trusted by 50,000+ websites.

Get SearchWP Now
Multiple Search Engines Icon