Optimizing WordPress Search Query Strings for Improved SEO-Friendly URLs
June 14, 2023 ⚊ 1 Min read ⚊ WORDPRESSSearch query strings are the parameters added to a URL when performing a search on a website. In WordPress, the default search URLs typically include the term “?s=” followed by the search query. For example, a search for “best WordPress themes” would result in a URL like “example.com/?s=best+WordPress+themes.”
However, such URLs are not particularly user-friendly or optimized for search engines. By modifying the search query strings, we can create more SEO-friendly URLs that convey the topic of the search and improve the overall user experience.
Inside the functions.php file, add the following code snippet to modify search query strings:
function wps_search_url_redirect() {
if ( is_search() && !empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" . urlencode( get_query_var( 's' ) ) ) );
exit;
}
}
add_action( 'template_redirect', 'wps_search_url_redirect' );
After adding this code, for the search of “best WordPress themes” the resulting URL will be like “example.com/search/best+WordPress+themes”.