WordPress Snippets at WPcustoms

Clean search url permalinks

Redirects search results from /?s=query to /search/query/, converts %20 to +


/**
 * Snippet Name: Clean search url permalinks
 * Snippet URL: http://www.wpcustoms.net/snippets/clean-search-url-permalinks/
 */
 function wpc_nice_search_redirect() {
  global $wp_rewrite;
  if (!isset($wp_rewrite) || !is_object($wp_rewrite) || !$wp_rewrite->using_permalinks()) {
    return;
  }

  $search_base = $wp_rewrite->search_base;
  if (is_search() && !is_admin() && strpos($_SERVER['REQUEST_URI'], "/{$search_base}/") === false) {
    wp_redirect(home_url("/{$search_base}/" . urlencode(get_query_var('s'))));
    exit();
  }
}
add_action('template_redirect', 'wpc_nice_search_redirect');