Open5

【PHP】header()を使ってリダイレクトを行った際のHTTPステータスコードについて

8rine238rine23

結論

header() を使ってリダイレクトを行った時は、ステータスコードが302になる

8rine238rine23

検証方法

  1. 以下のようなファイル(index.php, next.php)を用意する
  2. php -S localhost:8000 を実行してサーバを立ち上げる
  3. http://localhost:8000/index.php にアクセスする
8rine238rine23

index.php

<?php
header('Location: http://localhost:8000/next.php');
exit;

next.php

<?php
print('this is next page');
8rine238rine23

環境

❯ php -v
PHP 8.0.27 (cli) (built: Jan  6 2023 03:55:10) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.27, Copyright (c) Zend Technologies
    with Zend OPcache v8.0.27, Copyright (c), by Zend Technologies