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

WP REST API Get Media Detail By ID

WordPress WP REST API V2 get media attachment by id with php curl tutorial and code example

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 Auth

WP REST API Delete Post With PHP cURL

Wordpress tutorial WP REST API Delete Post With PHP cURL with code example

WP REST API V2 Auth

WP REST API Create Comment Into A Post

Wordpress Tuorial WP REST API Create Comment Into A Post.

WP REST API V2 Auth

WP REST API Create Post with jQuery Ajax

Wordpress WP REST API v2 tutorial create post with jquery ajax using basic authentication with code example