This guide explains how to customize the WooCommerce search results page URL using the Advanced Woo Search plugin. Learn how to keep your existing layout intact while transforming default URLs into more user-friendly formats like/search/query/ with a simple code snippet.
With Advanced Woo Search you can display search results in two ways - inside live AJAX results and via the standard search results page.
For the search results page, the plugin doesn’t create its own template - it simply uses your existing WooCommerce product search page and inherits its results. This guarantees that all your page styles and layout remain the same and nothing breaks.
The same applies to the search page URL - it will also look the same.
Default WooCommerce search page URL:
www.mysite.com/?s={search_query}&post_type=product
But what if you want to make it more user-friendly? Something like:
www.mysite.com/search/{search_query}/?post_type=product
Below, we’ll cover how to achieve that.
The solution is very simple. Just add the following code snippets inside your child theme’s functions.php file or use any plugin for adding custom code snippets:
add_action('init', function() {
add_rewrite_rule(
'^search/([^/]*)/?(.*?$)',
'index.php?s=$matches[1]&=$matches[2]',
'top'
);
});
add_action('template_redirect', function () {
if ( isset ( $_GET['type_aws'] ) && isset( $_GET['s'] ) ) {
$query_params = array();
parse_str($_SERVER['QUERY_STRING'], $query_params);
unset($query_params['s']);
$query_string = http_build_query($query_params);
wp_redirect(home_url('/search/') . $_GET['s'] . '/?' . $query_string, 301);
exit();
}
});
No, since the plugin inherits the existing WooCommerce search page, all your styles and layout will remain untouched.
Yes, after adding the rewrite rule, visit Settings → Permalinks and click Save Changes to flush the rewrite rules.
Yes, just replace the '^search/([^/]*)/?(.*?$)' part in the rewrite rule with your preferred URL structure.