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 WebLogToolsCollection.com: http://weblogtoolscollection.com/archives/2004/08/05/404-search-function-for-wordpress/

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

Imagine you have a site called:
“ram.kossboss.com/tasty-pizza”
But someone put in the URL bar:
“ram.kossboss.com/pizza-tasty”  or even “ram.kossboss.com/pizza” or even “ram.kossboss.com/pizza thats tasty” (note this last one is actually going to autofill to “ram.kossboss.com/pizza%20thats%20tasty“, 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.

THE STEPS

(STEP1 IF USING WORDPRESS)

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:

Make a file:

Then make it have the correct permissions:

To see & verify the permissions of everything

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,

(STEP1 IF USING APACHE WITHOUT WORDPRESS)

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:

 

(STEP2)

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)

 

(STEP3) 

Edit the line that says $search_url to match your domain and your search query variable.
You see the line that says:
$search_url = 'http://ram.kossboss.com/index.php?s='; Change the part that says 'http://ram.kossboss.com/index.php?s='  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
'http://ram.kossboss.com/index.php?s=' For you it might be
'https://www.mysite.com/index.php?query=' or
'https://www.mysite.com/index.php?q=' Anyhow modify that string that says $search_url = 'http://ram.kossboss.com/index.php?s=';  to have the proper thing in the single quotes, so for the above examples I would change it to this:
$search_url = 'https://www.mysite.com/index.php?query=' or
$search_url = 'https://www.mysite.com/index.php?q='

(STEP4)
Save & try it out.

Leave a Reply

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