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 Posts By Category

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

wp rest api v2

WP REST API Get Post By Slug With PHP

WordPress WP REST API Tutorial Get Post Data By Slug With PHP

wp rest api v2

WP REST API Get Post By Id With PHP

WordPress WP REST API V2 Tutorial get post by post id using php script.

WP REST API V2 Auth

WP REST API Upload Image Media

Wordpress WP REST API V2 tutorial and code example upload image / media with PHP cURL

WP REST API V2 Auth

WP REST API Basic Authentication With PHP Curl

Wordpress Tutorial WP REST API Basic Authentication With PHP Curl