Jump to content

Generating a user and google friendly 404 page


ProblemHelpPlease

Recommended Posts

I am trying to find the best way to generate a custom 404 page for a website. I can easily customize a 404 page to provide a user friendly interface for the user side but I also want to still generate a hard 404 error for search engines so that the custom 404 isn't indexed.The information I have found so far is conflicting as to the best approach to do this.I wondered what other people used to generate a custom 404 page whilst maintaining the 404 HTTP status code.

Link to comment
Share on other sites

use .htaccess files or, if you have access to the server configuration specify there.The idea is to check every time a request is made so that it is known if the file exists.If the file exists it's obvious.If the file does not exist, on the server side (only) redirect to a (for instance) php page that sends the 404 status code to the user and a normal HTML page showing the error.There are two main ways to do this.One is easier but a little less portable

ErrorDocument 304 /path/to/file.php

Where is /path/to/file.php you may use simple text (not recommended), the path to the php file relative to the root or a url to where it should get the document or redirect the user to.I don't remember the other way where you check the file existance, etc... Also it requires you to use mod_rewrite.c which may not be available

Link to comment
Share on other sites

I was looking at using the code you used but with 404 not 304. Is redirecting to a standard php page going to work as a hard 404 error or does the php page need to do something to generate the 404 for the server and the search engines.

Link to comment
Share on other sites

What I do if, for example, I have a query string to select the section and the user puts a section name that doesn't exist is to send a 404 header with PHP while displaying a decorated 404 page.Here's an overly simplified version of one of my pages:

<?php// $section is given by user input and is already sanitizedif(!file_exists('sections/' . $section . '.php')) {  header("HTTP/1.0 404 Not Found");  include ('sections/404.php');}?>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...