Search results URL without query string variables

It’s better to share some of your code that already you used.

And you mention first line that you have search page ? what exactly you mean by that ?

WordPress have default search page did you mention that one ? if you are then try this code

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

But if you have custom page with search slug then you have to do this little-bit different way. However built-in search use always good idea.
Still you want to display search result after page slug like
domain.com/page/search-term then first you have to make search form and pass form url with your specific page one. like <form role="search" method="get" class="search-form" action="<?php echo home_url( '/page-slug/' ); ?>"> and then use this code for finishing.

add_action( 'template_redirect', 'change_search_url_rewrite' );
function change_search_url_rewrite() {
    if (is_search()) {
        $search_url = get_bloginfo('url') .'/search/'. urlencode(get_query_var('s')) . ((get_query_var('paged')) ? '/page/'. get_query_var('paged') ."https://wordpress.stackexchange.com/" : "https://wordpress.stackexchange.com/");
        if (!empty($_GET['s']) || !empty($_GET['paged']))
            wp_redirect($search_url);
    }
}

hope it makes sense!

Leave a Comment