Do you want to change the default search URL slug in WordPress?
By default, WordPress search URLs are not user-friendly, so visitors may struggle to understand your site’s layout. They’re also not optimized for search engines, so you may miss out on visitors.
In this article, we will show you how to easily change the default search URL slug in WordPress so it’s more SEO and user-friendly.
Why Change Default Search URL Slug in WordPress
By default, WordPress uses SEO friendly URL structure for all the pages on your website. Typically, SEO-friendly WordPress URLs might look like this:
http://example.com/some-post/
http://example.com/2018/03/news-article/
http://example.com/category/some-category/
As you can see, these URLs are quite easy for visitors to understand. They can simply look at their browser’s address bar and see where they are in your site’s layout.
These URLs also tell search engines useful information about the page, so they can rank it correctly and show it to people who are looking for content just like yours.
However, a typical search URL in WordPress looks something like this:
http://example.com/?s=search-term
The extra ?s= characters make this URL more difficult to read and understand, which can confuse both the search engines and your visitors. By changing this URL, you can improve WordPress search and boost your SEO.
While some WordPress search plugins help you show more accurate and detailed search results, most plugins don’t change the search URL itself.
With that being said, let’s see how you can change the search URL slug and improve your WordPress SEO. Simply use the links below to jump straight to the method you want to use:
Method 1: Change WordPress Search URL Slug Using WPCode (Recommended)
The easiest way to change the default WordPress search slug is by using WPCode.
WPCode is the most popular code snippets plugin used by over 2 million WordPress websites. It allows you to add code snippets in WordPress without having to edit your site’s functions.php file.
The first thing you need to do is install and activate the free WPCode plugin on your website. For more details, see our step-by-step guide on how to install a WordPress plugin.
Upon activation, go to Code Snippets » Add Snippet.
This will bring you to the ‘Add Snippet’ page, where you can see WPCode’s library of ready-made snippets. These include snippets that allow you to improve your WordPress security by disabling XML-RPC, upload file types that WordPress doesn’t support by default, and more.
To add your own snippet, just hover your mouse over ‘Add Your Custom Code (New Snippet).’ Then, click on ‘Use snippet’ when it appears.
You need to start by entering a title for the custom code snippet.
This can be anything that helps you identify the snippet in the WordPress admin area.
Since you are adding a PHP snippet, open the ‘Code Type’ dropdown menu and choose ‘PHP Snippet.’
You can then go ahead and paste the following into the code box:
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
This code snippet replaces the ‘/?s=search-term’ characters with ‘search,’ so your slug will look something like: http://example.com/search/wordpress
To use something other than ‘search’ in your URL, simply customize the code snippet above. For example, you might want to add some relevant keywords to boost your SEO.
When you are happy with the code, it’s time to change where the snippet runs by scrolling to the ‘Insertion’ box.
To start, make sure ‘Auto Insert’ is selected. Then, open the ‘Location’ dropdown and choose ‘Frontend Only’ since we will only use this code on our site’s public-facing facing front end.
You can also add tags to help you sort the code snippets by topic and functionality.
When you are happy with how the snippet is set up, you can make it live by clicking the ‘Active’ toggle. Finally, don’t forget to click on ‘Save Snippet.’
Now, visit your site and perform a search.
If you take a look at your browser’s address bar, you’ll see the new SEO-friendly search URL.
Method 2: Change WordPress Search URL Slug via htaccess File
Another option is to edit your site’s .htaccess file. This method is more complicated, so it isn’t recommended for beginners. However, it does allow you to change the search URL slug without using a code snippet plugin.
To access the .htaccess file, you’ll need an FTP client such as FileZilla, or you can use the file manager of your WordPress hosting cPanel.
If this is your first time using FTP, then you can see our complete guide on how to connect to your site using FTP.
Once you’ve done that, open .htaccess and paste the following code at the bottom of the file:
# Change WordPress search URL
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]
This will change the WordPress search slug to the following:
http://example.com/search/your-search-query/
You can change this slug by customizing the code snippet.
Once you’ve done that, don’t forget to save your changes and upload the .htaccess file back to the server.
Now, if you perform a search on your site, you’ll notice that it’s using the new slug.
We hope this article helped you change the default search URL slug in WordPress. You can also go through our guide on how to increase your blog traffic and the best WordPress SEO plugins and tools.
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Syed Balkhi says
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
Brandon Boyd says
Hi, I just came across this today and it’s been super helpful. Is there anyway to remove the “You Search For” prefix in the page title when doing a search?
WPBeginner Support says
That would be added by your specific theme first. We would recommend checking with the support for your theme to see if they have a built-in or recommended way for changing that without needing to modify your theme’s files.
Admin
Kedar Bhogle says
Thank you Respected Sir/Madam,
I am looking for such code because I want to use theme default theme’s search box using “Google Custom Search Engine” for extra revenue. Finally I have done using this code (all credit goes to you Sir / Madam). I have basis knowledge about html ( I don’t learn @ any institution, I learned online….. Let’s go),
Very Very Thanks again…..
WPBeginner Support says
Glad our guide could help
Admin
mostafa elaissaoui says
thank you
this is helpful
WPBeginner Support says
You’re welcome
Admin
Josh says
Is there a way to change /search/ to /somethingelse/
WPBeginner Support says
You would use the first method and replace the word search with the word you wanted
Admin
Josh says
Works great. Any idea why this might leave off closing / as in url.com/search/search-term instead of /search-term/
WPBeginner Support says
For the first method, depending on your permalinks it may not automatically add a trailing slash, if you wanted one you would need to add the trailing slash to the wp_redirect function or modify your permalink settings.
Admin
Carlos says
Method 1 doesn’t work if I change “/search/” for another term. I triead “/busca/” that means search in portuguese…
Richard Talbot says
Carlos is right. Same goes for method 2 (htacess method. The only word it will work for is “search”. If you try any other word like “result”, “busca”, “whatever”, etc, then you get a 404.
Very confusing as to why it only works for the word “search”.
WPBeginner Support says
You may want to ensure you cleared your caching if you’ve edited the code from the article for a word other than search for one possible reason.
Admin
Allewar says
These methods alone won’t work, they only make the «pretty» slug, but the keyword you need to change is the WP rewrite rule for the query.
You can use this code in your theme’s functions.hp to use any word you want (also includes the rule for the ‘page’ slug):
function re_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->search_base = ‘buscar’;
$wp_rewrite->pagination_base = ‘pagina’;
$wp_rewrite->flush_rules();
}
add_action(‘init’, ‘re_rewrite_rules’);
Cristina says
I’ve used the .htaccess metod and it works. Ths a lot!
Joseph says
Do you meant that the search result page can be indexed by search engine?
However, the content in the search result page is a kind of duplicate content as what we can find in the archieve, category or tag pages.
PS. I am not an expert in SEO and might have wrong concept.
Linas says
Thank You! Works great. Used 1st method with functions.php