Vivienne Vivienne - 1 month ago 23
PHP Question

PHP header( "Location: /404.php", true, 404 ) does not work

I'd like to use the following to redirect pages that are no longer present in the database to the custom 404 page:

ob_start();
....
if ( !$found ):
header( "Location: /404.php", true, 404 );
exit();
endif;


But this actually does not redirect, but just shows an empty page (because of the exit() call before any output to the browser).

I've also tried the following:

if ( !$found ):
header( "HTTP/1.1 404 Not Found" );
exit();
endif;


With a 'ErrorDocument 404 /404.php' in my .htaccess file, but this also just shows an empty page.

And if I do this:

if ( !$found ):
header( "HTTP/1.1 404 Not Found" );
header( "Location: /404.php" );
exit();
endif;


It does redirect, but with a 302 header.

Any help would be greatly appreciated.

Answer

Maybe without redirect?

if ( !$found ):
   include('../404.php');
   exit();
endif;