Want to change the default search URL in WordPress? By default, WordPress’s search address isn’t welcoming it. Read on to see how to easily change the default search address alias in WordPress and make it more user welcoming.
Why change the default search URL alias in WordPress
WordPress uses SEO-friendly URL structures for all pages on your website. A typical SEO friendly
WordPress URL is as follows:
http://example.com/some-page/
http://example.com/2018/03/some-article/
http://example.com/category/some-category/
As displayed, these URLs are easy for users and search engines to understand. However, WordPress still uses unfriendly URLs as search results pages. A typical search address in
WordPress is as follows:
http://example.com/?s=search-term
This URL structure does not match other website URLs. Although some WordPress search plugins can improve search results, most people don’t do anything with search addresses.
Make your search URL look like:
http://example.com/search/your-search-term/
Let’s take a look at how to make WordPress search page URL aliases more friendly. Change search page URL alias in WordPress
For this tutorial, the editing of the WordPress theme file is required.
Method 1. Change the WordPress search address Slug through the function file
This method is relatively easy and is recommended for most users. First, copy and paste the following code into the theme’s functions.php file:
functionwpb_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’);
Don’t forget to save changes.
Now visit the website and try to use the search function. The search address now looks like this:
http://example.com/search/your-search-query/
Method 2. Change the search URL Slug through htaccess file
The .htaccess file is usually used to set up redirection and configuration tasks for other websites. It can also be used to change the default search address in WordPress. .htaccess file is located in the root folder of the website, edit it using the FTP or file manager application in cPanel.
In the .htaccess file, paste the following code at the bottom:
# Change WordPress search URL
RewriteCond% {QUERY_STRING} \\? S = ([^ &] +) [NC]
RewriteRule ^ $ / search /% 1 /? [NC, R, L]
Don’t forget to save the changes. Visit the website and use the search function. Search address now looks like this: http://example.com/search/your-search-query/