Redirect search results from ?s=term to /search/term/, which is cleaner to share and easier for caches and analytics.
add_action( 'template_redirect', function (): void {
if ( ! is_search() || ! isset( $_GET['s'] ) || isset( $_GET['post_type'] ) ) {
return;
}
$term = get_search_query( false );
if ( '' === $term ) {
return;
}
wp_safe_redirect( home_url( '/search/' . rawurlencode( $term ) . '/' ) );
exit;
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
WordPress search results live at /?s=your+search. WordPress also understands /search/your-search/, which looks cleaner, can be cached by CDNs that ignore query strings, and is easier to read in analytics.
When a search arrives with ?s=, the snippet redirects to the pretty version with the search term URL-encoded. WordPress already handles the /search/ path with pretty permalinks, so no rewrite rules are needed.
Searches with a post_type parameter, like WooCommerce’s product search, are left alone so their filters keep working.
Search for “hello world” with your theme’s search box. The address should become /search/hello%20world/ and show results.