【PHP】404エラーを正しく表示・処理する方法

HTML
スポンサーリンク

PHPで404エラーを正しく表示する方法の解説です。

存在しないページにアクセスされた時、ざっくり404エラーページにリダイレクトしたい場合は、
.htaccessに1行記載すればOKです。

例えば、以下のコードは、ページが見つからない場合、「正しく」404.htmlへリダイレクトしてくれます。

ErrorDocument 404 https://(ドメイン名)/404.html

これはこれで良いのですが、

例えば、もう少し特殊な例で、
PHPからMySQLのデータを呼び出した際に、不正なパラメータ値でアクセスされ、404ページを表示した場合。
PHPでどう記載すれば良いかの解説。

予め、自作404ページ「404.html」があるものとします。

スポンサーリンク

よく使われるHTTPステータスコード一覧

その前に、とりあえず、よく使われるステータスコードの一覧です。

ステータスコードステータス意味
200
OK成功!
301
Moved Permanently永久的にリダイレクトされた。
302
Moved Temporarily一時的にリダイレクトされた。
403
Forbiddenユーザーのアクセスが禁止された。
404
Not Foundページが見つからなかった。

まずは、ダメな例

header("HTTP/1.1 404 Not Found");
header("Location: "http://(ドメイン名)/404.html");
exit;

 

1行目で「HTTP/1.1 404 Not Found」を指定しているのは良いのですが、
次の「Location: 〜」で転送設定しているので、ステータスコードは「404」ではなく「302」となってしまいます。

一見、自分で用意したNotFound 404ページが表示されるので、
「404エラー処理してるのかなー」っと思いがちですが、
実際は、見た目だけ404で、検索エンジンは302として解釈されます。

302エラーは、リダイレクト先へ「一時的に」移動されたことを意味するので、
ファイルが見つからなかった404エラーとは全く別の意味となります。

HTTPヘッダーの確認(WEBサービスで確認)

以下のページから、HTTPヘッダーがどのようになっているか確認することができます。

ページ上部「URL/ドメイン」の欄にリダイレクトされる前のURLを入力してみてください。

上記の間違った例の場合は、302エラーになるはずです。

良い例は、ズバリこれ

header("HTTP/1.1 404 Not Found");
include($_SERVER['DOCUMENT_ROOT'].'/404.html');
exit;

ポイントは2行目で、
404.htmlへリダイレクトするのではなく、
現在のページに404.htmlを取り込む」というという構造になっています。

PHP関数にして使う場合の例

ページが多くあり、いろんなダメパラメーターでアクセスされる可能性がある場合は関数にしておくと何かと便利です。

<?php
if (何かの判断){
 err404();
}

function err404() {
    header("HTTP/1.1 404 Not Found");
    include($_SERVER['DOCUMENT_ROOT'].'/404.html');
    exit;
}
?>

注意点としては、
404ページを外部ファイルとして取り込む構造上、エラー判断は、<html>タグ(または<!doctype html>)の前で行ってください。

また、自作404ページは、どのタイミングで呼び出されても良いように、ファイルパスは相対パスではなく、絶対パスで指定してください。

例:

<!-- ダメな例  -->
<img src="img/title.png">

<!-- 良い例 -->
<img src="/img/title.png">
<img src="http://xxxxxx.com/img/title.png">

コメント

rtp live

rtp slot

slot bet 200

bonus new member

rtp slot gacor

situs slot777

joker123

idn poker

situs joker123

rtp

slot bet kecil

slot bonus

depo 25 bonus 25

baccarat online

slot server thailand

sbobet

slot garansi kekalahan

slot bonus new member

sbobet88

slot bonus

slot777 gacor

situs slot bet kecil

slot777

slot bonus new member 100

slot777 gacor

rtp slot gacor hari ini

slot bet

slot88

slot88 resmi

slot gacor hari ini

slot777 login

slot88

slot gampang maxwin

rtp slot tertinggi

starlight princess

bonus new member

wisdom of athena

mochimon

slot 200

slot bet 200

slot777 online

slot777 gacor

jurrasic kingdom

sugar rush

aztec gems

bonus new member

mahjong ways

bonus new member 100

caishen wins

caishen wins slot

slot777 gacor

tree of riches slot

aztec slot

lucky neko slot

rtp slot

slot gacor

slot88 resmi

situs slot

https://slotbonus.softcia.com.br

https://slotbonus.deparmotor.com/

bonus new member

slot777

rtp slot

slot 777

bonus new member

https://adresa-telefony.ru/img/slot777/

https://adresa-telefony.ru/img/joker123/

https://adresa-telefony.ru/img/sbobet/

https://rtp-slot.nwbc.com.au/

https://realesaletter.com/js/sbobet/

https://realesaletter.com/js/slot777/

https://www.yg-moto.com/wp-includes/bet-kecil/

https://pri-israel.com/wp-includes/bet-kecil/

https://www.contato10.com.br/wp-includes/slot-bet-kecil/

https://copima.jp/latex/bet-100/

https://www.routlens.lu/app/themes/bet-100/

https://aws-klinkier.pl/wp-content/bet-100/

https://esigarauzmani.org/wp-includes/slot-bet-kecil/

https://www.bonus-new-member.albedonekretnine.hr/

slot bet 100

slot bet 100

slot bet 100

タイトルとURLをコピーしました