Making Pretty WordPress Search URLS is better for SEO

Most people should be using at least some form of pretty permalinking with their WordPress system. It should almost be set as a default these days given the way SEO is. Most would notice when pretty permalinks is enabled and you have a search page, that the URL doesn’t follow the rules. It seems to ignore everything around it and still runs off the “?s=%QUERY%” string.

I don’t know about you, but if I have the rest of the site using pretty permalinks, I want the search page to follow.

This tutorial will show how to easily transform your WordPress search urls into a more readable and friendly rewrittend structure instead of the default url. It’s better for SEO since the search result page URLs will now be SEO friendly.

With a default installation of WordPress and when url rewriting is enabled the search url’s remain unaffected. The default WordPress search query for “WooCommerce” for example outputs the url like below.

https://haloseeker.com/search/?q=WooCommerce

What this tutorial will do is change the output of your WordPress search urls using the WordPress wp_redirect function to look like the example below which as you can see is a much prettier url output.

https://haloseeker.com/search/WooCommerce

Solution 1: Open your themes functions.php file and paste the code below into it, save that file then go check out your new search urls.

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

Solution 2: It is also possible via htaccess rules, but the source is an example for custom solutions on an redirect.

# search redirect
# this will take anything in the query string, minus any extraneous values, and turn them into a clean working url
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]

This would go directly into the .htaccess file. Now there is a way to do the above without modifying the .htaccess file directly and thats by using the WP_Rewrite class to add the rule. Something along the lines of (add to functions.php in you theme):

add_action('init','haloseeker_rewrite_rules');
function haloseeker_rewrite_rules() {
 global $wp,$wp_rewrite;
 $wp_rewrite->add_rule('search/([^/]+)/', 'index.php?s=$matches[1]', 'top');
 $wp_rewrite->add_rule('search/([^/]+)/page/([^/]+)/', 'index.php?s=$matches[1]&paged=$matches[2]', 'top');
// Once you get working, remove this next line
$wp_rewrite->flush_rules(false); 
}

This method is quite clean and leaves all modifications within the wp-content folder.

So now if you type into your browser yourdomain/search/keyword it should take you to your search page as normal. The trick is that the form needs to use the same structure. This would obviously require a custom search form and functionality to do so.

Solution 3: Use Custom Search URL Plugin to replace default search page syntax “?s” in wordpress search page with the word you choose. Or use Nice Search Plugin to redirect search URLs from ?s=yourkeywords to /search/yourkeywords and converts spaces to + symbols.

I would recommend to put the function in a plugin or in the functions.php of your theme. Only use wordpress plugin if you really unknown about code because of using many plugins will make you wordpress site runs slowly. Any other or better ideas to accomplish this? Please let us know in the comment area.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
jang seo hee Recent comment authors
  Subscribe  
Notify of
jang seo hee
Guest

Hey there would you mind letting me know which web host you’re working with?
I’ve loaded your blog in 3 different browsers and I must say this blog loads a lot faster then most.
Can you recommend a good hosting provider at a honest price?

Thanks, I appreciate it!