This tutorial will show how to easily transform your WordPress search urls into a more readable and frieindly rewrittend structure instead of the default url which contains ?s= and without using any additional WordPress plugins.
With a default installation of WordPress and when url rewriting is enabled the search url’s remain unaffected. The default WordPress search query for wordpress plugins for example outputs the url like below.
http://sitespex.com/?s=wordpress+plugins&submit=Search
What this tutorial will do is change the output of your WordPress search urls usuing the WordPress wp_redirect function to look like the example below which as you can see is a much cleaner url output.
http://sitespex.com/search/wordpress+plugins
Open your themes functions.php file and paste the code below into it, save that file then go check out your new search urls.
function search_url_rewrite(){ if(is_search() && !empty($_GET['s'])){ wp_redirect(home_url("/search/"). urlencode(get_query_var('s'))); exit(); } } add_action('template_redirect', 'search_url_rewrite');
Let us know if you have any improvments or have any errors with this code snippet in the comments.
Leave a Reply
You must be logged in to post a comment.