How to Change the Default Search URL Slug in WordPress

Affiliate Disclaimer: This site is supported by its visitors. We may earn a commission for purchases made through the links on our site, this helps to keep everything running smoothly. Learn More

Last Updated on September 20, 2023

How to Change the Default Search URL Slug in WordPress

As a default feature, WordPress Search URLs aren’t optimized for search engines, or people.

In this post, we will demonstrate how you can easily alter the default search URL in WordPress to make it more user-friendly and SEO friendly.

Why Change Default Search URL Slug in WordPress

As a default feature, WordPress uses SEO-friendly URL structures for all pages on your site.

They provide search engines with important information about the site that can aid them in ranking your website properly and displaying the relevant results in Google.

But, a normal web search in WordPress appears like this:

The additional?s+ characters make this website more challenging to comprehend and can cause confusion for the search engines and your site visitors.

While certain WordPress search plugins can help you provide more precise and thorough search results, the majority of plugins do not alter the URL of your search.

In that regard, let’s look at how you can alter the URL of your search.

1. Use WPCode to Change the WordPress Search URL Slug

The most straightforward method to alter your default WordPress search slug is to use WPCode.

WPCode, previously known as Insert Headers and Footers by WPBeginner, lets you add code snippets to WordPress without the need to modify your website’s functions.php file.

The first thing you have to do is to install the plugin ->

After activation after activation, go to Code Snippets > Select Snippet.

You will be taken to the “Add Snippet” page, where you can view the WPCode library of ready-to-use short snippets.

We need to include our own custom code, so use the option ‘Add your Custom code (New Snippet).

It is important to begin by creating a title for the code snippet that you have created.

It could be anything that aids you in identifying the code in the WordPress admin area.

Because we’re adding a PHP Snippet, go to the dropdown menu ‘Code Type’ and select ‘PHP Snippet.’

Then, you can copy and insert the following code in the box for code:

function wpb_change_search_url() {
If ( is_search() and ! empty( $_GET[‘s’] ) )
wp_redirect( home_url( “/search/” ) . urlencode( get_query_var( ‘s’ ) ) );
add_action( ‘template_redirect’, ‘wpb_change_search_url’ );

This code snippet replaces the ‘/?s=searchterm’ characters with ‘search,’ so your slug will look something like:

If you want to use something other than search within your website’s URL you can modify the code in the above snippet.

If you’re satisfied with your code, you can modify the location where the code is running by scrolling into the ‘Insertion’ field.

For starters, ensure that Auto Insert is selected. After that, you can open the dropdown menu for ‘Location’ and select “Frontend Only” because we’ll use this code on our website’s front-end.

It is also possible to assign tags for your fragment. This can help you organize your code snippets according to subject and function.

Then hit “Save Snippet” and set it to active. Make sure to save by clicking on the Update button.

That should do it, feel free to try it out.

2. Modify the WordPress search URL Slug using htaccess

Another option is to modify your .htaccess file. This is more complex and isn’t recommended for beginners.

For access to .htaccess files, you’ll require an FTP client, such as FileZilla, or you could make use of the file manager in the file manager of your WordPress hosting cPanel.

Once you’ve found it, just add the following code at the end of the file.

# Modify WordPress Search URL
RewriteCond %QUERY_STRING ?s =([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]

This will change WordPress search slugs to be the following:

After doing this, remember to back up your modifications and upload the .htaccess file back onto the servers.

And that’s it, I hope this helped.

More Posts