Guide for Changing Your WordPress Website’s Default Search URL Slug

Guide for Changing Your WordPress Website’s Default Search URL Slug

default search URLThe default search URL slug is not as user-friendly. This is because WordPress assigns a URL by default. In this post, we shall be taking a look at the main reason why you should change the default URL and how you can do that.

Why change the URL?
WordPress adheres to SEO friendly URL structure for all web pages. The standard SEO friendly WordPress URLs look something like:

As you may have guessed, the URLs are easy to understand for the users and search engines. However, some of the default search URLs used by WordPress are not SEO friendly. A typical default site name goes like this:

As you can see, the URL doesn’t match your website’s URL. While some of the WordPress search plugins will improve search results, not all of them will. The good news is that you can manually edit the URL so that it is friendly. Here are the steps you need to take to change the default search URL slug.

Changing using the Functions File
This is the most recommended method when changing the default URL. You will start by copying and pasting this code to the functions.php file or the site-specific plugin.

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’
) ;

Save the changes. Upon using the search feature, you will notice that the URL looks as follows:
     

Using .htaccess file
The .htaccess file is mostly used to setup redirects and a range of other WordPress website configurations. It can also be used to change the default search URL slug. This file is located in the root folder of your website. To edit it, you will need to use File Manager app or FTP in the cPanel. Paste the following code in the .htaccess file.

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

Save the changes afterwards and upload the .htaccess file back to the server. Your URL will look like this:
           

These are the two methods you can use to change the default search URL slug of your website. Upon making the changes, you will gradually start noticing an improvement in your web traffic.

Default search URL