I have a site where the article is shown by its ID. Something like this:

mysite.ru/article.php?id=123 

I need to make sure that when a non-existent article shows me the page specified in .htaccess in the same way:

 ErrorDocument 404 /page404.php 

But the link remained the same. Which side it can crank, tell me, please?

  • It is necessary to put all the processing of the URL on the script, not htaccess. You receive an URL if there is no page with such URL, send the necessary headers and create the content 404 pages manually. Pseudocode: if ($ site-> page_load ($ url) {header ("Status: 200 OK"); $ view-> render_page ();} else {header ("Status: 404 Not Found"); $ view-> render_page404 ();} - MDJHD
  • In php, OOP is somehow poorly implemented, unlike the same Java. + I have already heard more than once that when using OOP, performance drops dramatically. Therefore, I use the procedural method, even against my own will. Besides, I didn’t want to get involved in .htaccess. But then how is it really possible to identify a non-existent file and give an error? - Vitaly Zaslavsky
  • one
    @vital_viza, did you "hear about a drop in performance", or encountered it? (I am silent, that this example is written in five minutes and in the OP-paradigm) - etki
  • I heard. There was no need to collide, because so far there are no such large projects. But the risk is not worth it. + I do not like OOP in PHP. Therefore, I am not going to use it - Vitaly Zaslavsky
  • @vital_viza, but I wrote that this is pseudocode, write as you like. Nonexistent file? You chtoli file store pages? If yes, then file_exist () will help you if (file_exist ($ path)) {header ("Status: 200 OK"); $ view-> load_page ($ path); } else {header ("Status: 404 Not Found"); $ view-> load_page ($ path_to_404_html); } - MDJHD

1 answer 1

Well, I think that as such there is no way to find out the standard 404 error page, so I did this trick:

In .htaccess I wrote the following lines that intercept the main errors:

 ErrorDocument 400 /errors.php?code=400 ErrorDocument 403 /errors.php?code=403 ErrorDocument 404 /errors.php?code=404 ErrorDocument 405 /errors.php?code=405 ErrorDocument 408 /errors.php?code=408 ErrorDocument 500 /errors.php?code=500 ErrorDocument 502 /errors.php?code=502 ErrorDocument 504 /errors.php?code=504 

It's all clear. The errors.php script gets the error code via GET and displays the text we need.

And here is the function that creates the error. Anyone that I would need

  function throw_error($code = 404) { //проверяем, отправлены ли заголовки if (!headers_sent($filename, $linenum)) { $code = int($code); //коды основных ошибок $codes = array( 400 => "400 Bad Request", 403 => "403 Forbidden", 404 => "404 Not Found", 405 => "405 Method Not Allowed", 408 => "408 Request Timeout", 500 => "500 Internal Server Error", 502 => "502 Bad Gateway", 504 => "504 Gateway Timeout", ); //убеждаемся, что такая ошибка есть в массиве $status = "404 Not Found"; if (isset($codes[$code])) { $status = $codes[$code]; } else { $code = 404; } //отправляем заголовки $sapi_name = php_sapi_name(); if ($sapi_name == 'cgi' || $sapi_name == 'cgi-fcgi') { header('Status: '.$status); } else { header($_SERVER['SERVER_PROTOCOL'] . ' '.$status); } /* Костыль, знаю, зато работает. Вывод страницы с ошибкой Не include, потому что на страницах используются константы, которые имеют широкую область видимости. */ echo file_get_contents('http://'.$_SERVER['HTTP_HOST'].'/errors.php?code='.$code); exit(); } else { //если заголовки уже отправлены, говорим об этом echo "Sorry... Headers already sent on line $linenum"; exit(); } } 
  • and this person is talking about performance ... echo file_get_contents ('http: //'.$_SERVER [' HTTP_HOST '].' / errors.php? code = '. $ code); - zb '
  • @eicto okshschi - etki
  • @eicto, I would not throw this code here if I didn’t need advice. + if all the same OOP in PHP is not as productive as many say, then using this crutch once will not really reduce performance. And besides, I said that the code uses constants, which, when included with the error page, would give out a lot of notices because of this. So I used these crutches. To rewrite the whole code, for the sake of some kind of check for the existence of an article or some other record ... Well, you know ... Somehow lazy) - Vitaly Zaslavsky
  • 2
    @vital_viza are you serious? Your engine restarts itself and still strains the network (even if it is a loopback) and the server. And certainly not fcgi and php is reinitialized each time. microtime(true) and forth. > And besides, I said that the code uses constants that, when included with the error page, would give out a lot of notices because of this. What is this engine, which constants are prevented from giving an error message? > Rewrite all the code, for the sake of some kind of test for the existence of an article or some other record ... Therefore, it should be rewritten for convenience. - etki
  • Cope with the inclusion. So everything seems to be working fine - Vitaly Zaslavsky