Wrote a script site, addresses of the following type Vasya.ru / 1234567

I looked for the search, a bunch of non-existing pages of the Vasya.ru / contact.html Vasya.ru / contact / appeared and many other options, while the page actually does not exist, but the code 200 is given.

The .htaccess file looks like this

## Application Handler RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)?$ 2.php?a=$1 [QSA,NC,L] ## Error 404 ## ErrorDocument 404 /index.php?a=404 

How to teach the script to expose 404 error on non-existing pages?

The site itself is formed using cURL requests , because of this, many non-existing pages appear ...

Help to understand, thanks!

Added after the script that displays the result using echo $ postResult; code ...?> <?php header('HTTP/1.0 404 Not Found'); ?> ...?> <?php header('HTTP/1.0 404 Not Found'); ?> now 404 is given to the server. But if there is a page, an error message appears - the header has already been transmitted ie 404 tries to work, but as a result, 200 is OK as it should. It is possible to do with the help of else, so that when the answer is 200, 404 is not given? Thank!

  • one
    ErrorDocument 404 /index.php?a=404 and what do you think why this rule? - ArchDemon
  • I think that this is a redirect that will work when receiving 404 errors - MicroRu
  • one
    @MicroRu in this index.php return 404 code with a = 404 - Alex78191
  • I do not know how to return a 404 error if there is no page, now it returns 200 approx. Some kind of condition must be written to everyone except Vasya.ru / 1234567 Vasya.ru / 1000567 Vasya.ru / 1234567000, etc. counted as 404 - MicroRu
  • Added after the script that displays the result using echo $ postResult; code ...?> <?php header('HTTP/1.0 404 Not Found'); ?> ...?> <?php header('HTTP/1.0 404 Not Found'); ?> now 404 is given to the server. But if there is a page, an error message appears - the header has already been transmitted ie 404 tries to work, but as a result, 200 is OK as it should. It is possible to do with the help of else, so that when the answer is 200, 404 is not given? Thank! - MicroRu

1 answer 1

Add the following code to the beginning of your 2.php file:

 // в этот массив добавьте ссылки на все несуществующие страницы $nonExistentPages = array('contact.html', 'contact'); if (in_array($_REQUEST['a'], $nonExistentPages)) { header("{$_SERVER['SERVER_PROTOCOL']} 404 Not Found"); // если нужно выдавать какой-то информационный контент, // делайте это здесь. exit; } 
  • The fact is that there may be hundreds of these pages and they are not known to me in advance. I receive them through cURL ... I also really need your code for another purpose. Thank! For now, I’m thinking of inserting the index meta tag, nofolow - MicroRu
  • one
    My main goal was to answer your question "How to return a 404 error if the page does not exist." And by what criteria you will determine whether it exists or not - decide for yourself. The variant with checking by array is given by me for an example. - P. Fateev