PHP에서 오류 404를 생성하려면 어떻게해야합니까?
내 .htaccess는 모든 요청 /word_here
을 /page.php?name=word_here
. 그런 다음 PHP 스크립트는 요청 된 페이지가 페이지 배열에 있는지 확인합니다.
그렇지 않은 경우 오류 404를 어떻게 시뮬레이션 할 수 있습니까? 나는이 시도하지만 통해 구성 내 404 페이지가 표시되지 않은 ErrorDocument
에 .htaccess
게재.
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
내 오류 404 페이지로 리디렉션하는 것이 잘못되었다고 생각하는 것이 맞습니까?
404 페이지 생성에 대한 최신 답변 (PHP 5.4 이상 기준)은 다음을 사용하는 것입니다 http_response_code
.
<?php
http_response_code(404);
include('my_404.php'); // provide your own HTML for the error page
die();
die()
꼭 필요한 것은 아니지만 정상적인 실행을 계속하지 않도록합니다.
수행중인 작업이 작동하고 브라우저에 404 코드가 수신됩니다. 이것이 하지 않는 것은 예상 할 수있는 "찾을 수 없음"페이지를 표시하는 것입니다. 예 :
찾을 수 없음
요청한 URL /test.php를이 서버에서 찾을 수 없습니다.
PHP가 404 코드를 반환 할 때 웹 서버가 해당 페이지를 보내지 않기 때문입니다 (적어도 Apache는 그렇지 않습니다). PHP는 모든 자체 출력을 전송합니다. 따라서 유사한 페이지를 원하면 HTML을 직접 보내야합니다. 예 :
<?php
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found", true, 404);
include("notFound.php");
?>
httpd.conf에 입력하여 자체 404 메시지에 대해 동일한 페이지를 사용하도록 Apache를 구성 할 수 있습니다.
ErrorDocument 404 /notFound.php
이 시도:
<?php
header("HTTP/1.0 404 Not Found");
?>
.htaccess 파일을 통해 사용자 지정 오류 페이지 만들기
1. 404-페이지를 찾을 수 없음
RewriteEngine On
ErrorDocument 404 /404.html
2. 500-내부 서버 오류
RewriteEngine On
ErrorDocument 500 /500.html
3. 403-금지됨
RewriteEngine On
ErrorDocument 403 /403.html
4. 400-잘못된 요청
RewriteEngine On
ErrorDocument 400 /400.html
5. 401-인증 필요
RewriteEngine On
ErrorDocument 401 /401.html
모든 오류를 단일 페이지로 리디렉션 할 수도 있습니다. 처럼
RewriteEngine On
ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
ErrorDocument 403 /404.html
ErrorDocument 400 /404.html
ErrorDocument 401 /401.html
헤더를 보낸 후 die ()를 기억 했습니까? 404 헤더는 처리를 자동으로 중지하지 않으므로 추가 처리가 발생하면 아무 작업도 수행하지 않은 것처럼 보일 수 있습니다.
It's not good to REDIRECT to your 404 page, but you can INCLUDE the content from it with no problem. That way, you have a page that properly sends a 404 status from the correct URL, but it also has your "what are you looking for?" page for the human reader.
try putting
ErrorDocument 404 /(root directory)/(error file)
in .htaccess
file.
Do this for any error but substitute 404 for your error.
In the Drupal or Wordpress CMS (and likely others), if you are trying to make some custom php code appear not to exist (unless some condition is met), the following works well by making the CMS's 404 handler take over:
<?php
if(condition){
do stuff;
} else {
include('index.php');
}
?>
Immediately after that line try closing the response using exit
or die()
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
exit;
or
header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
die();
참고URL : https://stackoverflow.com/questions/1381123/how-can-i-create-an-error-404-in-php
'IT story' 카테고리의 다른 글
자바 스크립트 : 다음 5의 배수로 올림 (0) | 2020.08.29 |
---|---|
Objective-C를 사용하여 런타임에 선택기를 어떻게 동적으로 만들 수 있습니까? (0) | 2020.08.29 |
Pandas DataFrame에 tsv 파일을로드하는 방법은 무엇입니까? (0) | 2020.08.29 |
스프링 범위 프록시 빈 (0) | 2020.08.29 |
Android 애플리케이션에서 두 개의 SQLite 테이블을 조인하려면 어떻게해야합니까? (0) | 2020.08.29 |