Custom url search result WordPress


With WordPress it is easy to activate readable URLs. Only the search page is not affected by the setting yet, it comes with the syntax ?s as default. But there is a solution to change the URL as you wish.

The following example shows that even if you have permalinks activated, that the URL for the two searchterms wordpress and consulting looks like this: With a little function, which communicates with the redirect, you can adjust the URL. In my case the URL with the two searchterms look like this with the following function:

Within WP-function wp_redirect I set the output, that the term search will be added to the home-url of your installation and added by the searchterms. There are many possibilities to adjust the URL.

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

Yes, it is also possible via htacces 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]

I would recommend to put the function in a Plugin or in the functions.php of your theme. Any other or better ideas to accomplish this? Please let us know in the comment area.



Change the Search-URL of WordPress