0

I have made a custom error 404 page and have used .htaccess to use that using: ErrorDocument 404 "error_page.php"

Now the problem is that I have a profile.php page where I show users profiles. If a username mentioned in the URL doesn't exist then I redirect to index.php. Instead I want to show an error 404 page with my custom error page. Will this be enough to do that effectively?

header("HTTP/1.1 404 Not Found");
include 'error_page.php';
exit();

Thank you.

user2471133
  • 1,377
  • 3
  • 14
  • 15
  • This will have the same effect as Apache's error page, however no log entry will be made in the error log. That may or may not be relevant for you. – Pekka Jun 10 '13 at 13:49
  • I want to have the same effect as when a user visits a page that doesn't exists. How can I do that? ie, how can I have it in the error log? I thought showing the header will also put an entry in the error log. – user2471133 Jun 10 '13 at 13:51

1 Answers1

1

It should work, but you could also redirect to the error page (but you'd lose the current URL).

header('Location: /error_page.php');
die();

EDIT: If you really want it in the error log, a cheap (and ugly) way is to redirect to a non-existant page:

header('Location: /404'.$_SERVER['REQUEST_URI']);
die();

That way you'd get logs with /404/path/to/profile.php?userid=9999 so you get an indication of what was requested.

Alexandre Danault
  • 8,602
  • 3
  • 30
  • 33
  • I knew about this, but this will not give error 404 to search engines. and also the user will not know what he actually typed in the url. – user2471133 Jun 10 '13 at 13:53
  • If you want search engine to know this is a 404, you have to place `header("HTTP/1.1 404 Not Found");` at the top of your error_page.php file. – Alexandre Danault Jun 10 '13 at 13:56
  • If you really want it in the error log, the preferred way to do it is to use the [`error_log` function](http://www.php.net/manual/en/function.error-log.php) – nstCactus Jun 10 '13 at 14:07
  • So the code that I have provided above is fine? should I just add the error_log function to it? – user2471133 Jun 10 '13 at 14:24
  • From my experience, error_log() seems to work/not work base on how php is called (SAPI, CGI, etc) and ends up logging at the wrong places (system log, event log) depending on your setup. Also the logging format will be different from real 404's unless you take great care at making your entries exactly like apache's. – Alexandre Danault Jun 10 '13 at 14:35