I have a 404 file and it is working with nginx. However when I modify the 404 file to redirect the user it does not redirect.

visiting http://site.com/404.php redirects the user.

visiting http://site.com/askllj299lakj is just a blank page.

The code in the 404 file is just a header redirect,

header('Location: http://site.com/');

Why doesn't this work in nginx?

  • Try removing the php from the 404.php file add some basic text, then visit http://site.com/askllj299lakj and see if you can view the text. If yes, then try using php echo to display the same text and see if that works. Are you using PHP FastCGI
    – Anagio
    Jun 8, 2012 at 9:29
  • And look in your PHP error log. A space before the first <?php would cause these symptoms and would leave messages about it in your error logs.
    – Ladadadada
    Jun 8, 2012 at 22:48

1 Answer 1


A redirect is by definition a 302 header code which will result in a new request. 404 error pages should never redirect anywhere as they are an end of the line code. In addition if you where to do this and redirect the user to the homepage with a 404 error that would cause all sorts of issues later on down the track as search engines would treat your homepage as a 404 not found error page.

Instead of redirecting to the homepage in this matter create a standard HTML page that says that the page the user was searching for could not be found on the server (you can play with it and make it humorous if your website is appropriate for that). On that HTML page there is nothing stopping you from redirecting the user to the homepage using...

<meta http-equiv="refresh" content="3;url=http://www.domain.com" />

Which will redirect the user to the homepage after 3 seconds. This tells the end user the page they where searching for couldn't be found and redirects the user back to the homepage and complies with all standards and doesn't cause later problems down the track.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.