0

I have a search form, which redirects to the result. If that page does not exist, I want to display a 404 error. How can I do that?

My code

<center>
    <input name="txtSearch" maxlength="12" size="25" type="text" class="field" id="txtSearch" />
    <div>&nbsp;</div>

    <div class="button" style="display:inline-block">
        <a  onclick='window.location.href="" + document.getElementById("txtSearch").value;'>
            Submit
        </a>
    </div>
</center>
Rando Hinn
  • 1,255
  • 19
  • 41
  • What framework are you using? –  Aug 13 '17 at 19:31
  • I am not. using. I have only a solid background yet. –  Aug 13 '17 at 19:33
  • https://stackoverflow.com/questions/14421303/php-nearest-string-comparison Here is a solution for comparing nearest matching string you can create an array of names for files that you have and loop through it. – 3gth Aug 13 '17 at 19:40
  • @3gth thank you. Now, how can I redirect all of wrong searches to one single page? Exemple www.domain.com/inexistentpage i want it to redirect automaticaly to www.domain.com/404 ? –  Aug 13 '17 at 19:43
  • refer this https://stackoverflow.com/questions/14838488/redirect-to-a-php-html-file-on-404-error-with-htaccess – 3gth Aug 13 '17 at 19:44
  • @3gth how can I make .htacces if I'm doing everything with XAMPP on localhost? –  Aug 13 '17 at 19:46
  • create .htaccess file in your applications root directory and write rules for 404 redirection – 3gth Aug 13 '17 at 19:49

1 Answers1

0

Firstly, it appears, that there are some strong points that are wrong in your idea and possibly understanding of search. As your post and code shows, you only allow users to search for specific things. Correct me, if I'm wrong and your code operates in other ways, but..

Imagine this: a 10 year old boy from Kenya ends up on your site - how is he supposed to know what content your site contains? Or if he does, how is he going to try and find it? Yup, he uses search.

The first hit he gets, is a 404, because he searched for something that does not exist, and does not know that your site only allows to search for specific things, that exist. He leaves your site, unable to find the content he wanted.

This is not search's function. Search should allow you to search for everything, display a list of all possible results, and a polite error if none are found - definately not reditrect to a specific page immediately - instead, a generic page for all searches, or serve a 404 if said super-specific page does not exist.

I reccommend you look into Algolia or other similar products to facilitate search.

To fix your question's problem(s), however...

  1. You have a missing quotation mark at the class declaration of the button div. Will edit the Q to fix that.
  2. You really, really need to use indendation. It will make your code so much easier to read, and better and more appealing for others to help you. Will edit that aswell.
  3. What you need, is a .htaccess file in your project root, with a defined 404 page, at the very minimum. Something like this, edited, to fit your 404 error page path and name. You will design and develop your 404 page as any other page. This will, hovever apply to ALL of your missing pages, so style and build it's content appropriately (see why a search results page is better, now?).

    ErrorDocument 404 /404.html

  4. If you happen to use nginx instead of apache, do this instead in your server configuration:

    error_page 404 /404.html;

  5. Alternatively, you could build a form around the search, and send the query string (which is a fancy word for the string the user searches for) to some PHP script of yours, which could determine if there are results to show, or not and display appropriate content - again, this could also be done with Algolia or similar - just parse and feed it your content in an appropriate form.

Rando Hinn
  • 1,255
  • 19
  • 41