Your Gateway To Digital Success
Wesbytes Blue Logo

Wesbytes Knowledge Base

Search our articles or browse by category below

How to Change the Default Search URL Slug in WordPress

Last modified: July 2, 2022
You are here:
Estimated reading time: 1 min

Change the Default Search URL Slug in WordPress

However, you may change the search URL to match your site’s permalink structure with a few easy changes. search page optimization for search engines

Amend Your Site's Functions File

Editing the functions.php file on your website is the simplest approach to alter the slug for your search page. You can use cPanel, an FTP client, or any local text editor to alter the file after downloading it. To modify the file directly on the server, use the File Manager in the HostPapa Dashboard.

Head to your WordPress site root, then select wp-content > themes.  Open the directory pertaining to your active WordPress theme.

Select wp-content then select themes

Copy and paste the following code at the bottom of the functions.php file:

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

Change search page slug

Save the file (and upload to the server, if required), then head to your site’s front-end and use your search feature. Notice how the URL structure has changed to http://www.mydomain.com/search/search-term.

Change Search URL Slug Using .htaccess

Using a.htaccess rule is an alternate way to update the search URL slug. The Apache web server‘s configuration file,.htaccess, can be used to change the format of URLs.

Your WordPress root folder contains the.htaccess file. Use cPanel File Manager once more to edit the file directly on the server, or download using an FTP client and edit with a text editing programme.

Change Search URL Slug Using .htaccess

Add the following code to the .htaccess file:

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

Save the file (and upload to the server, if required), then head to your site’s front-end and use your search feature. Notice again how the URL structure has changed.

Was this article helpful?
Dislike 0
Views: 5