My picture
Monday, October 21, 2019
2006 | 04 | 19 | Blog | Dev | WordPress
Search
Content
BY TYPE
BY CATEGORY
BY DATE
BY PUBLICATION
Updates
FEEDS
MAILING LIST
Enter your email address above to start
receiving email updates from this site.
This Page
Post Meta
TAGS
TRACKBACKS AND INBOUND LINKS

TechPush

Simpler WordPress Search URLs
April 19, 2006

Thanks to some under-the-hood black magic, newer versions of WordPress support a streamlined permalink structure for search URLs. (In some corners, these are known as “cruft-free” URLs.)

WordPress-powered URLs ending in /search/foo (where “foo” is your search term) now work the same way as the old-school format of index.php?s=foo. They’re cleaner, more intuitive, and darned easier on the eyes.

The only problem? The search form included in most templates produces old-school URLs instead of the newer ones, and the various ways of recoding the search form (see last year’s discussion on the wp-hackers mailing list) seem more trouble than they’re worth.

Thankfully, there’s a simple, elegant workaround using the WordPress WP_Query class. Just add the code below to the very top of your header.php template and say goodbye to ugly search URLs.

<?php
if ((is_search()) && ($_GET['s'])) {
	wp_redirect(get_bloginfo('url')."/search/".get_query_var('s'));
} //if
?>

As usual, standard disclaimers apply.