Change Search Permalink WordPress

By | June 7, 2017

Change / rewrite redirect wordpress search URL /permalink / slug with the words you want.

In out there there are some wordpress tutorials how to rewrite search permalink / slug. Most ( all ) of them give example rewrite default search from example.com/?s=query into website.com/search/query. You can see the slug in the search page is “search“. But when I try change it into another words it ‘s not worked ( in the latest wordpress version 4.7x).

Here is the code to change default search permalink into “search” slug.

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

if you want to rewrite your website search page into “search” slug, that function is enough. But what about if you want to change it into another slug? In my small experiment I want to change default search permalink into website.com/browse/query. The results is 404 or not found.Then I found additional rewrite function that can make your rewrite function worked. ( the function above is only redirect )

Here is the additional function.

function rewrite_search_slug() {    
    add_rewrite_rule(
        'browse(/([^/]+))?(/([^/]+))?(/([^/]+))?/?',
        'index.php?s=$matches[2]&paged=$matches[6]',
         'top'      
    );
}
add_action( 'init', 'rewrite_search_slug' );

Change the word “browse” with word you want. Don’t forget also change the word “search” in the first function as same as word you choose in the 2nd function and save the permalink.

Related Posts

WP REST API V2 Auth

WP REST API Get Featured Image URL

WordPress Tutorial WP REST API Get Featured Image URL in a Post

WP REST API V2 Auth

WP REST API Basic Authentication PHP File Get Contents

Wordpres Tutorial WP REST API Basic Authentication PHP File Get Contents. At previous post I already posted about wp rest api basic authentication with php cURL, but in some cases a hosting / server doesn’t has cURL module ( not installer , not enabled ).

wp rest api v2

Disable WP Rest API V2

Disable Wordpres WP JSON – WP Rest API V2 with and without plugin

WP REST API V2 Auth

WP REST API Delete Post PHP File Get Contents

WordPress WP REST API Delete Post Tutorial With PHP File Get Contents Function

wp rest api v2

WP REST API Get Posts By Category

WordPress REST API tutorial get posts by category id with php script