Title: 404 Search Function for WordPress
Last modified: August 18, 2016

---

# 404 Search Function for WordPress

 *  [Laughinglizard](https://wordpress.org/support/users/laughinglizard/)
 * (@laughinglizard)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/)
 * This is a modification of the 404 function found at [NSLog](http://nslog.com/archives/2003/02/26/404_search_function_code.php).
   This is tweaked such that if a page is not found on your WordPress blog, the 
   part of the URI beyond the domain name is stripped off and fed into the WordPress
   search function. If only one post if found, that post is directly loaded. Otherwise
   the whole list of posts is loaded and displayed.
    Learn more about it here: [http://weblogtoolscollection.com/archives/2004/08/05/404-search-function-for-wordpress/](http://weblogtoolscollection.com/archives/2004/08/05/404-search-function-for-wordpress/)

Viewing 15 replies - 1 through 15 (of 20 total)

1 [2](https://wordpress.org/support/topic/404-search-function-for-wordpress/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/404-search-function-for-wordpress/page/2/?output_format=md)

 *  [davidprince](https://wordpress.org/support/users/davidprince/)
 * (@davidprince)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79709)
 * That is really cool. I am adding it now.
 *  [davidprince](https://wordpress.org/support/users/davidprince/)
 * (@davidprince)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79745)
 * Here is the answer if your blog is not in the root directory.
    ` <? $URI_prefix
   = "/blog/"; $search_preterm = str_replace($URI_prefix,"",$_SERVER['REQUEST_URI']);
   $search_term = substr($_SERVER['REQUEST_URI'],1); $search_term = stripslashes(
   $search_preterm); $search_term = urldecode(stripslashes($search_term)); $search_url
   = 'http://davidblog.ws/blog/index.php?s='; $full_search_url = $search_url . $
   search_term; $full_search_url = preg_replace('/ /', '%20', $full_search_url);
   $full_page = implode("", file($full_search_url)); print_r($full_page); die();?
   >  Change /blog/ to whatever subdirectory your blog is in. Also.. You may want
   your .htacess in your blog dir to call a slightly different 404.php file (I call
   404blog.php) that way the search text is formatted correctly regardless of whether
   the original 404 occurs in your root or blog directory. Carl.. Your problem is
   because the code on original page is styled (in some way) on the authors page.
   Therefore you need to strip the styling or just copy what is below.  <? $search_term
   = substr($_SERVER['REQUEST_URI'],1); $search_term = urldecode(stripslashes($search_term));
   $search_url = 'http://davidblog.ws/blog/index.php?s='; $full_search_url = $search_url.
   $search_term; $full_search_url = preg_replace('/ /', '%20', $full_search_url);
   $full_page = implode("", file($full_search_url)); print_r($full_page); die();?
   >
 *  [carlbanks](https://wordpress.org/support/users/carlbanks/)
 * (@carlbanks)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79746)
 * Thanks David! This is an awesome hack.
 *  [davidprince](https://wordpress.org/support/users/davidprince/)
 * (@davidprince)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79748)
 * Damn.. Neither peice of code is stripping tailing slashes..
 *  [davidprince](https://wordpress.org/support/users/davidprince/)
 * (@davidprince)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79750)
 * Hmm.. maybe not stripping a trailing slash is a good thing.
 *  [carlbanks](https://wordpress.org/support/users/carlbanks/)
 * (@carlbanks)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79752)
 * [http://carlbanks.triplehelix.info/firefox](http://carlbanks.triplehelix.info/firefox)
   
   My posts have firefox in them, but it’s not returning anything.
 *  [davidprince](https://wordpress.org/support/users/davidprince/)
 * (@davidprince)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79754)
 * That is odd.
    It seems to be showing your 404.php file in the address. See this
   and compare [http://davidblog.ws/blog/firefox](http://davidblog.ws/blog/firefox)
   404.php does not show up in the address so something is messed somewhere. Is 
   the code you added to .htacess exactly like the example. I don’t think a full
   path to 404.php worked for me.
 *  [davidprince](https://wordpress.org/support/users/davidprince/)
 * (@davidprince)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79770)
 * Check out my above link to see how I am calling the search to a newly created
   search.php. 🙂 I think it gives the lost user an understanding of what is wrong.
 *  [carlbanks](https://wordpress.org/support/users/carlbanks/)
 * (@carlbanks)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79778)
 * How did you do that?
 *  [divrom](https://wordpress.org/support/users/divrom/)
 * (@divrom)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79791)
 * That’s a cool addition, David!
 *  [davidprince](https://wordpress.org/support/users/davidprince/)
 * (@davidprince)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79793)
 * I made the 404.php call search.php, which is almost identical to index.php except
   I added the red text at the top of the page..
    Here is the call to that page 
   in 404.php: `$search_url = 'http://davidblog.ws/blog/search.php?s=';` That is
   the only line that is changed. The red text is added to a #topmain division where
   my breadcrumb is called.
 *  [davidprince](https://wordpress.org/support/users/davidprince/)
 * (@davidprince)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79853)
 * Sorry.. How exactly did you do that?
    I am slow, so please speak slowy. 🙂
 *  [davidprince](https://wordpress.org/support/users/davidprince/)
 * (@davidprince)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79861)
 * Ah.. Okay I understand. Pretty cool I will whip something up.
 *  [Ruby Sinreich](https://wordpress.org/support/users/rubyji/)
 * (@rubyji)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79918)
 * So I don’t use the WP search function (I use free [Atomz](http://atomz.com) search),
   but since I just migrated by whole blog over from MT and changed the URLs I thought
   this would be pretty handy. I changed this so that it just spits out a searchable
   term that I automatically put into the search box on my 404 page.
    It goes like
   this: `<? $search_term = substr($_SERVER['REQUEST_URI'],1); $find = array ("'.
   html'", "'[-/_]'") ; $replace = " " ; $new_search = preg_replace ( $find , $replace,
   $search_term ); echo $new_search ; ?> (I should probably figure out a way for
   it to remove the directory in the URL it’s getting, but I’m lazy and this works
   OK. I tried a lot of variations on "'/.+/'" but I got nothing. Any suggestions?)
   You can see it in action with this old URL: [http://orangepolitics.org/about/one_community_many_voices.html](http://orangepolitics.org/about/one_community_many_voices.html)
 *  [NuclearMoose](https://wordpress.org/support/users/nuclearmoose/)
 * (@nuclearmoose)
 * [21 years, 9 months ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/#post-79919)
 * You could also check out Binary Bonzai’s Kubrick web kit…it has a great 404 and
   search page.

Viewing 15 replies - 1 through 15 (of 20 total)

1 [2](https://wordpress.org/support/topic/404-search-function-for-wordpress/page/2/?output_format=md)
[→](https://wordpress.org/support/topic/404-search-function-for-wordpress/page/2/?output_format=md)

The topic ‘404 Search Function for WordPress’ is closed to new replies.

 * 20 replies
 * 8 participants
 * Last reply from: [sva](https://wordpress.org/support/users/sva/)
 * Last activity: [21 years ago](https://wordpress.org/support/topic/404-search-function-for-wordpress/page/2/#post-80064)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
