0

I have a code

<?php
header('Location: http://mydomain/404', true, 404);
exit;
?>

which does nothing. Outputs with a white screen and I can watch 404 response status, but no redirect. Ubuntu 12.04 / PHP 5.3

Same problem on server with Debian Squeeze / PHP 5.3

What I do wrong?

Update.

It is a part of function that takes status and path. I need a redirect to a specified page with a specified status code. Not direct display 404 page.

2
  • Does this url exist for your domain http://mydomain/404 Commented Apr 25, 2013 at 6:12
  • Sure, it is a page handled by Apache Rewrite. Commented Apr 25, 2013 at 6:13

3 Answers 3

8

You can't have a Location redirect for a 404. The correct behavior would be to include the 404 page in the result with a 404 header.

<?
header( $_SERVER['SERVER_PROTOCOL']." 404 Not Found", true, 404 );
include('404.php');
?>

That is unless the 404 page is on another domain. In which case you can simply use a Location redirect without the 404 like Apache does by default when it can't find your error document.

Sign up to request clarification or add additional context in comments.

1 Comment

Seems that I understood problem.
4

It's either a 404 Not Found or a 302 Redirect, it can't be both at the same time. If you want to signal This Page Does Not Exist, use a 404. If you want to redirect the user, use a 302.

2 Comments

You mean I can not do redirect using Location header with status != 301/302?
Exactly! The status codes have a specific meaning for what the client should do. 3xx codes are redirects, 4xx codes are errors.
0

Do this via .htaccess

<IfModule mod_rewrite.c>
RewriteEngine On
ErrorDocument 404 /404.html
</IfModule>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.