How to repurpose 404 pages from displaying “page not found” to instead searching for page. So that instead it will do a search through your website’s php URI search feature.

Thank you to Mark Ghosh from

This will work with anything that has a search function using php and a URL search string.

Imagine you have a site called:
But someone put in the URL bar:
“”  or even “” or even “ thats tasty” (note this last one is actually going to autofill to ““, however dont worry it will still search correctly with this 404 search feature)

Normally this would return a 404 error page as the page pizza-tast or pizza or “pizza thats tasty” doesnt exists
But what this will do to your site is make that 404 error go away, and it will bring up the search result.



If using wordpress first make child theme (they make it so your edits dont get lost when an upgrade fails, and its easier to keep track of your theme edits, as they are in a different directory)

Make a child theme for your current theme (its super simple, but no time to explain again – just look at link: HERE[how to make child theme from 2014 theme] and HERE[how to make child theme] and HERE[my child theme article – just scroll down to it when you click on the link])

If your already using a child theme for your own additions/edits, then your good to go

Then log in via SSH (or use a wordpress plugin to make a file in the child theme called 404.php – a plugin such as Better File Editor can bring light to your edits, not sure if it can make a file in your theme, but it can definitely edit your files and give syntax highlights) to make a file called 404.php
Go to the location where your theme is:

cd /var/www/wp-content/themes/YOURTHEME-child/

Make a file:

touch 404.php

Then make it have the correct permissions:

chown www-data:www-data 404.php

To see & verify the permissions of everything

ls -lisah

Read up on permissions to see how chown and chmod works: HERE(basics) and HERE(chmod and chown) and HERE(chmod and chown) and HERE(copying permissions) – the idea is you want to make the permissions of the 404.php the same as the rest of your pages that are working properly,


If using apache2:
Make a 404.php on the root of your site (or backup your current 404 page and make it not work anymore)
Then edit your .htaccess on the root of your site to say this on a new line:

ErrorDocument 404 /404.php



Edit the file, either with “nano” or “vi” or “vim” or using the wordpress theme editor (Note I strictly recommend the following plugin to make your editing of themes and plugins much more enjoyable:  try a plug in called Better File Editor, a php and html editor with style[syntax highlighting etc…] – this requires your file to have the correct permissions www-data:www-data by default in all apache2/wordpress files or else you wont be able to save your edits)

Then put this exact content in the page (you can edit the comments, just if your using wordpress dont edit the ones with @ in them, unless your parent theme is not twentyfourteen, then change the @subpackage Twenty_Fourteen to the @subpackage seen in your themes php files – if your not using wordpress you can either choose to keep or delete the comments with the @ signs – wordpress using those for metadata for administrating the wordpress themes)

* 404 SEARCH
* KOSSBOSS EDIT: convert - and _ and %20 to spaces in the search
* ---
* The template for displaying 404 pages (Not Found)
* ---
* But searching for the uri part after the
* @package WordPress
* @subpackage Twenty_Fourteen
* @since Twenty Fourteen 1.0
/*Code for 404.php*/
$search_term = substr($_SERVER['REQUEST_URI'],1);
$search_term = urldecode(stripslashes($search_term));
$willbespaces = array("_","-","%20");
$search_term = str_replace($willbespaces, " ", $search_term);
$search_url = '';
$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();



Edit the line that says $search_url to match your domain and your search query variable.
You see the line that says:
$search_url = ‘’;
Change the part that says ‘’  to match your websites search string (to find it out, do a search for anything on your site right now, and then look at the URL bar you should see something like a “?s=” without quotes, basically you might have an
“s” as you see in “?s=”, or maybe a “q” for query or maybe a query instead of an “s”, or maybe even a search, or maybe even a “foot” so it would say “?foot=”… either way thats your search variable… you need everything left of that search variables equal sign)
So for me its
For you it might be
Anyhow modify that string that says $search_url = ‘’;  to have the proper thing in the single quotes, so for the above examples I would change it to this:
$search_url = ‘’
$search_url = ‘’

Save & try it out.

Leave a Reply

Your email address will not be published. Required fields are marked *