Reputation: 681
I want to transform all 404 urls into search strings. This is easily done by adding this to 404.php:
$search_term = substr($_SERVER['REQUEST_URI'],1);
$search_404 = str_replace('/','+',$search_term);
$search_404 = str_replace('-','+',$search_404);
$location = "http://example.com/?s=" . $search_404;
session_start();
$_SESSION['search_404'] = true;
header("location: $location");
There should also be some sanitization, but that's not the point right now.
What I want further is to do without the "?s=", so the url stays unchanged but have the search still work. Is this possible/how?
(I don't worry too much about SEO, as the pages are noindexed anyway, but I do worry about potential conflicts with existing pages or security holes)
Update: Apparently, this should be simple if wp itself would use search as last resort when everything else fails, i.e. if there's no post, page, category, tag etc. with a given url, start the search query... is there a way to alter the template hierarchy so as to put search last?
Upvotes: 1
Views: 90
Reputation: 681
OK, it looks like one way around this is to ignore the search template and use the very 404 template instead - like this:
$search_term = substr($_SERVER['REQUEST_URI'],1);
$search_404 = str_replace('/','+',$search_term);
$search_404 = str_replace('-','+',$search_404);
$args=array('s' => $search_404, );
query_posts($args);
if(have_posts()) { while (have_posts()) : the_post();
// WHATEVER
endwhile; wp_reset_query(); }
The thing does the job now.
Am still curious if there may be any conflicts or security holes and if the page still returns a 404 or not.
Upvotes: 1