PHP & MySQL Conference : Apache configuration File



Apache’s ErrorDocument directive can come in handy. For example, this line in your Apache configuration file:

ErrorDocument 404 /error.php

Can be used to redirect all 404 errors to a PHP script. The following server variables are of interest:

  1. $REDIRECT_ERROR_NOTES – File does not exist: /docroot/bogus
  2. $REDIRECT_REQUEST_METHOD – GET
  3. $REDIRECT_STATUS – 404
  4. $REDIRECT_URL – /docroot/bogus

Don’t forget to send a 404 status if you choose not to redirect to a real page.

<? Header('HTTP/1.0 404 Not Found'); ?>

Interesting uses

  1. Search for closest matching valid URL and redirect
  2. Use attempted url text as a DB keyword lookup
  3. Funky caching

An interesting way to handle caching is to have all 404’s redirected to a PHP script.

ErrorDocument 404 /generate.php

Then in your generate.php script use the contents of $REDIRECT_URI to determine which URL the person was trying to get to. In your database you would then have fields linking content to the URL they affect and from that you should be able to generate the page. Then in your generate.php script do something like:

<?php $s = $REDIRECT_URI; $d = $DOCUMENT_ROOT; // determine requested uri $uri =
substr($s, strpos($s,$d) + strlen($d) + 1); ob_start(); //
Start buffering output // ... code to fetch and output content from DB ... $data = ob_get_contents(); $fp =
fopen("$DOCUMENT_ROOT/$uri",'w'); fputs($fp,$data); fclose($fp); ob_end_flush(); // Flush and turn off buffering ?>

So, the way it works, when a request comes in for a page that doesn’t exist, generate.php checks the database and determines if it should actually exist and if so it will create it and respond with this generated data. The next request for that same URL will get the generated page directly. So in order to refresh your cache you simply have to delete the files.

freeprogrammingebooks.com/download-programming-ebook/php-mysql
computer-books.us

Download PHP & MySQL Conferenc

Random Posts



eXTReMe Tracker

Free Ebook is proudly powered by WordPress | Bob |

Leave a Reply

You must be logged in to post a comment.

Designed by Posicionamiento Web |
who's online
Sponsored by Refinance & Debt Consolidation