Learn, Practice, and Excel with our Coding Tutorials!

Optimizing WordPress Search Query Strings for Improved SEO-Friendly URLs

June 14, 2023 1 Min read WORDPRESS

Search 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' ) ) ) );
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”.

Related Post:
Add Line Breaks to Woocommerce Product SKU & Category Name − We can easily add line breaks between product SKU and category name on the single product page. This formatting will make your product’s single page.
Check WordPress Content Exists or Not Before Post − Are you want to post from front-end of the WordPress website? You can check whether your content already exists in the database or not using.