Changing the search URL?

I want to change the search URL from example.com/search/keyword to example.com/result or topic/keyword.

Related posts

Leave a Reply

2 comments

  1. You can change the /search/ part to something else, like /topic/, but it will be harder to remove it completely, since that will conflict with WordPress Pages (is /banana/ a Page with the title “Banana” or is it the result page for the search term “Banana”?).

    To change the part, hook into the init action and change the $wp_rewrite->search_base variable:

    add_action( 'init', 'wpse21549_init' );
    function wpse21549_init()
    {
        $GLOBALS['wp_rewrite']->search_base = 'topic';
    }
    

    You will need to place this snippet of code in your theme’s function.php file, or in a new file that you create and that you place in the /wp-content/plugins/ directory. After you did this, visit the Permalinks setting page to flush the rewrite rules.

    However, by default when you submit a search you will end up on a URL like ?s=Banana. There are plugins that change this (Ramukar linked to one, but it has the /search/ part hardcoded and has other issues), and Hudson Atwell linked to a simple approach (even too complicated now: you don’t need to add the rewrite rule to Apache, as WordPress already contained them since version 1.5).