Generally, WordPress search URLs aren’t easy to remember or optimized for search engines by default. Since SEO directly impacts your site’s ranking on platforms like Google and Bing, adjusting these default settings can significantly improve your rankings.
Why Should You Change the Default Search URL Slug in WordPress?
WordPress automatically uses an SEO-friendly URL structure for all your pages and posts on your website.
For example:
- http://example.com/post-url/
- http://example.com/2018/02/post-url/
- http://example.com/category/post-url/
These URLs are easy for visitors to understand, allowing them to quickly see where they are within your site. Additionally, these descriptive URLs provide search engines with valuable information, helping them rank your content accurately and match it with relevant search queries.
However, when users perform a search on your WordPress website, the URL often appears as:
- http://example.com/?s=search-term
The inclusion of ?s= makes the URL less readable and user-friendly. It can confuse both visitors and search engines, potentially impacting your site’s usability and SEO performance.
By modifying the default search URL slug, you can create cleaner, more intuitive URLs, enhance the search experience for your visitors, and improve your site’s SEO.
Changing the default search URL slug can offer several advantages:
1. SEO Benefits
Search engines like Google prefer clean URLs with clear context about a page's content. For example, a URL such as http://example.com/search/wordpress is more SEO-friendly than http://example.com/?s=wordpress. A clean search slug can improve indexing and help your site rank better in search results.
2. Enhanced Brand Consistency
Polished, organized URLs enhance your site's overall professionalism and trustworthiness. Conversely, messy URLs can detract from your site's quality and credibility.
For example, if someone searches for "mobiles" on an online store, a URL like http://example.com/search/mobiles is easier to remember and share with others compared to a cluttered, technical-looking alternative.
Updating your search URL structure can create a better user experience while aligning with SEO best practices.
How to Change the Default Search URL Slug in WordPress?
Method 1: Change WordPress Search URL Slug via functions.php
1. Log into your cPanel account.
2. Navigate to Files > File Manager.
3. Navigate to the functions.php file by following this path: public_html > wp-content > themes > your_theme_folder.
4. Right-click on the functions.php file and choose Edit.
5. Paste the below code at the end of the file.
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' );
6. Click on Save Changes.
Now, when you perform a search on your site, you'll see that it’s using the new slug.
Method 2: Change WordPress Search URL Slug via .htaccess
1. Log into your cPanel account.
2. Go to Files > File Manager.
3. Inside the public_html, open the .htaccess file.
If you can't find the .htaccess file, in the upper right side of the page, click on Settings and enable the "Show Hidden Files" option.
4. Paste the following code:
# Change WordPress search URL slug
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]
5. Click on Save Changes.
Method 3: Change WordPress Search URL Slug Using WPCode
1. Log into your WordPress dashboard.
2. Go to the Plugins > Add New Plugin.
3. Search for the WPCode plugin, click on Install Now, and Activate it.
4. Go to Code Snippets » Add Snippet.
This will take you to the 'Add Snippet' page, where you'll find WPCode's library of pre-built snippets.
5. To create your snippet, hover over 'Add Your Custom Code (New Snippet)' and click on + Add Custom Snippet when it appears.
6. Choose PHP Snippet.
7. On the ‘Create Custom Snippet’ page, enter a title for your custom code snippet.
8. Next, paste the following code into the ‘Code Preview’ 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 portion of the URL with search, resulting in a cleaner URL like: http://example.com/search/wordpress.
If you want to use a different word instead of search, simply customize the above code snippet.
Once you're satisfied with the code, scroll to the ‘Insertion’ box to set where the snippet will run.
1. Ensure that ‘Auto Insert’ is selected.
2. Open the ‘Location’ dropdown and choose ‘Frontend Only,’ as this code will only be used on the public-facing side of your site.
When configuring everything, activate the snippet by toggling the ‘Active’ switch.
Finally, click ‘Save Snippet’ to apply your changes.
Visit your site and search for something.
Check your browser’s address bar, and you’ll notice the updated SEO-friendly search URL.