Jul 7, 2011

Wordpress Search URL Rewrite

Have you ever considered your wp-site search URL?

http://yoursite.com/?s=searchterm

Also after setting the custom permalink structure, the only URL which is not effected is the search URL, it still remains the same.
OK, now goto your site, and after your root URL, just type this search/term

http://yoursite.com/search/term

Woila!!!! you'll find that the search worked, just that there is no URL rewrite rule written for this.
So to make it done, just add the following function to your themes functions.php file

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


This function will now rewrite every search results URL. Which will something look like this:-

http://yoursite.com/search/term

I have just implemented this technique on Webs Tutorial.

No comments :

Post a Comment