2

Trying to redirect a page to my custom 404 error document, but in vain. Heres the code

header('HTTP/1.1 404 Not Found', true, 404); 

But it stays on the same page even though the header information changes as required

HTTP/1.1 404 Not Found
Date: Wed, 09 Jan 2013 18:10:44 GMT
Server: Apache/2.2.21 (Win32) mod_ssl/2.2.21 OpenSSL/1.0.0e PHP/5.3.8 mod_perl/2.0.4 Perl/v5.10.1
X-Powered-By: PHP/5.3.8

PHP page continues and no redirect is achieved!

sanchitkhanna26
  • 2,143
  • 8
  • 28
  • 42
  • 1
    Show us the code that you use to redirect. – Salman A Jan 09 '13 at 18:18
  • @SalmanA here it is -: ErrorDocument 404 /errors/junk.php – sanchitkhanna26 Jan 09 '13 at 18:19
  • "Not Found" does not implicit a redirect. When issued from PHP it will just be returned to the browser. Apaches `ErrorDocument` can only handle missing files in its own realm. – mario Jan 09 '13 at 18:20
  • So is there a workaround @mario. How do i redirect to my custom page – sanchitkhanna26 Jan 09 '13 at 18:21
  • should i use header("Location: /errors/junk.php"); – sanchitkhanna26 Jan 09 '13 at 18:24
  • @RayZ dude... is that `header()` code in the file /errors/junk.php? Because it sounds like everything is working as expected. – Sammitch Jan 09 '13 at 18:26
  • No @Sammitch it is in other file named "request.php". Is it working fine on your end – sanchitkhanna26 Jan 09 '13 at 18:27
  • @RayZ `ErrorDocument 404 /errors/junk.php` is an apache directive and will *only* trigger if apache can't find the file. In what you are attempting apache gets a request for `request.php` find the file, and serves it. Just because the page issues a 404 header on its own doesn't mean apache is going to do anything. Just point your browser at something like http://yoursite.com/klasjdaskdj.php and you'll see your custom 404 page. – Sammitch Jan 09 '13 at 18:31

4 Answers4

2

You should just do header("Location: /errors/junk.php"); as that's essentially what Apache does with custom error documents, just on the server level instead of in PHP. I believe Apache uses a 301 redirect, but I could be wrong.

Kyle
  • 1,757
  • 1
  • 12
  • 22
  • Yes, you are wrong. Apache 3xx redirects if the error document points to a _remote_ url. – Salman A Jan 09 '13 at 18:29
  • @SalmanA what do you think. Should i go with 'header("Location: /errors/junk.php");' – sanchitkhanna26 Jan 09 '13 at 18:31
  • OK so I was wrong about Apache using 301 for error pages, Salman A is correct it's only for remote URLS. But I only used it as an example of what is going on. You should still use header("Location: /errors/junk.php"); – Kyle Jan 09 '13 at 18:32
  • I think it is better to go with header("Location: /errors/junk.php"); as nothing else if working around. But thanks everyone for your help – sanchitkhanna26 Jan 09 '13 at 18:33
  • @Kyle Just wonder, if you're wrong, why didn't you update your question to reflect it? Edits aren't really that hard. – TARDIS Maker Apr 28 '16 at 22:38
2

Your apparent file structure:

/
  .htaccess
  request.php
  ...
  errors/
    junk.php

.htaccess

ErrorDocument 404 /errors/junk.php

request.php

header('HTTP/1.1 404 Not Found', true, 404);
echo "Despite the 404 header this ~file~ actually exists as far as Apache is concerned.";
exit;

errors/junk.php

header('HTTP/1.1 404 Not Found', true, 404);
echo "The file you're looking for ~does not~ exist.";
echo "<pre>" . var_export($_SERVER, TRUE) . "</pre>";
exit;

http://yoursite.com/request.php will show:

Despite the 404 header this ~file~ actually exists as far as Apache is concerned.

http://yoursite.com/filethatdoesntexist.php will show:

The file you're looking for ~does not~ exist.

[a dump of $_SERVER which may be helpful in writing custom 404 handler code]

If you have a file that exists, but you want it to pretend it's a 404 you can either write the redirect in PHP as:

header('Location: http://mysite.com/errors/junk.php');
exit;

Which will redirect the browser to the full URL, or simply:

include('errors/junk.php');
exit;

Which will leave the user at the same page URL, but present your error code.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
1

Do not use 3xx redirects for error pages. All they do is confuse search engines into thinking that the page exists at a different location. You can try this approach:

header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
require_once("errors/404.php");
die;

Modify the error page so that it can be executed directly (e.g. when Apache handles the 404 error itself) or included (inside your scripts).

If include_once is not an option, you could do a:

header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
echo file_get_contents("http://yoursite.com/errors/404.php");
die;

This action will remain invisible to the end-user.

Salman A
  • 262,204
  • 82
  • 430
  • 521
1

If you're using FastCGI you can not send a 404 response header by

header('HTTP/1.1 404 Not Found', true, 404);

Instead you have to use

header('Status: 404 Not Found');

Additionally this header('Status:...') instruction can not be combined with header('Location:...'). Thus in case of FastCGI the following code will give the correct 404 response code AND redirect to the custom 404 page:

header('Status: 404 Not Found');
echo file_get_contents('http://www.yoursite.com/errors/custom404.html');
exit;