Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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,

<?php
header('Location: http://site.com/');
exit;
?>

Why doesn't this work in nginx?

share|improve this question
    
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 '12 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 '12 at 22:48

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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