music
OSdata.com: programming text book 

OSdata.com

404 page not found redirects

summary

OSdata RSS
News Feed


OSdata blog RSS.

OSdata blog RSS - add to Bloglines
OSdata blog RSS - add to Google Reader.
OSdata blog RSS - add to My AOL
OSdata blog RSS - add to Newsgator.

    This discusses how to create a 404 header to redirect on page not found.

license

    This is example code from OSdata, This Side of Sanity, and Twiddledom, released under Apache License 2.0.

    Copyright © 2012, 2013, 2014 Milo

    Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

previous page   next page
previous page
virtual pages
  next page
timer and clock
Google

404 page not found redirects

summary

    This discusses how to create a 404 header to redirect on page not found.

404 Not Found

The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.

Status Code Defintions

    The normal method for creating a 404 redirect when a page isn’t found is to add an .htaccess rule (or the equivalent in the httpd.conf file).

ErrorDocument 404 http://www.example.com/404.php

restoring 404 redirects

    In an earlier lesson we created virtual pages. This means that we have mapped all requests using mod_rewrite and therefore as far as the server is concerned the initial request has been fulfilled. That is, we have hijacked any possible 404 redirects.

    This means we have to manually restore 404 redirects.

    Because our example file is still small, I will repeat the entire new file here (copy and paste into a file named index.php).

    Step (1): Save the initial request file.

    Step (2): Send requests for the home page to index.php.

    Step (3): Send requests for an alternative known web page to that web page. Repeat this step as often as needed. We will inttroduce a better method later in these lessons.

    Step (4): For all other attempts, send out the 404 header and then display the standard 404 web page.

<?php

//error_reporting(E_ALL);

/* FILE NAME /index.php */

/* step 1 */
$incomingurl = $_SERVER['REQUEST_URI'];

/* step 2 */
if ( ( $incomingurl == '/index.php' ) OR ( $incomingurl == '/' )
  {
    require_once('homepage.php');
    exit;
  }

/* step 3 */
elseif elseif ( $incomingurl == '/identifiedpage.php' )
  {
    echo 'successfully at test page<br>';
    echo $incomingurl;
    exit;
  }

/* step 4 */
header("HTTP/1.0 404 Not Found");
include_once("404.html");
exit;

    You can use the bold section (step 4) anywhere that you need to manually perform a 404 redirect. Note that all headers (not just 404) must be sent before any output to the browser. Otherwise you will get the dreaded “Cannot modify header information - headers already sent” message. Send headers before any echo or other output. Even a stray space character or newline will cause failure.

404 page

    Your 404 page should explain that people have reached a file that does not exist. These pages often include some kind of cute image. Your 404 page should also include navigation back to your home page ( a simple link will do) and any other special navigation that might help people get back on track and be happy with your website. I have heard that some versions of the Chrome web browser will fail to display a 404 page that is less than 512 characters.

    For now, creation of the 404 page is left as an exercise for the student. I am likely to add an actual example at some time in the future.

installation

    Copy and paste the block of code and store in a file named index.php.

    The index.php file is also available in the source code section at index.php, but it contains the whole index.php file rather than the simple example listed above. Emphasis: This page will include more code than shown in the example.

return to source code lessons

return to Outrageous Coder
return to Source Code Examples

previous page   next page
previous page
virtual pages
  next page
timer and clock

contact
comments, suggestions, corrections, criticisms

because of ridiculous spam attacks,
contact through Twitter (@OutrageousCoder) will be more reliable than the contact form

please contact us

your name:
email address:
phone number:
message:

return to Outrageous Coder
return to source code lessons
return to table of contents
free downloadable college text book

license

    This is example code from OSdata, This Side of Sanity, and Twiddledom, released under Apache License 2.0.

    Copyright © 2012, 2013, 2014 Milo

    Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

previous page   next page
previous page
virtual pages
  next page
timer and clock

Google


Made with Macintosh

    This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

Viewable With Any Browser


    †UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

    Names and logos of various OSs are trademarks of their respective owners.

    Copyright © 2014 Milo

    Created: May 23, 2014

    Last Updated: May 23, 2014


return to table of contents
free downloadable college text book

previous page   next page
previous page
virtual pages
  next page
timer and clock