26278 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Yahoo! Japan says
22 MEELLION User
IDs may have been
nabbed
Nintendo throws
flaming legal
barrel at YouTubing
fans
Optus outlines its
4G future
Hold our tiny
silicon spheres,
say gravity wave
detection
scientists
EMC vuln gives mere
sysadmins the power
of storage admins
Four Anons cuffed
in Italy
IBM gives a cloudy
outlook for COBOL
Bureau of Stats
releases
educational
SimClone game
I know who "Satoshi
Nakamoto" is, says
Ted Nelson
Google builds
crowdsourcing into
new Maps code stack
Slashdot
Ask Slashdot:
Wiring Home
Furniture?
Medical Firm Sues
IRS For 4th
Amendment Violation
In Records Seizure
Military Dolphins
Discover 1800s
Torpedo
Apple Mobile
Devices Cleared For
Use On US Military
Networks
Mice, Newts
Retrieved After a
Month Orbiting
Earth At 345 Miles
Up
IBM Takes System/z
To the Cloud With
COBOL Update
Google"s Nexus Q
Successor Hits the
FCC
Yahoo Board
Approves a $1.1B
Pricetag For Tumblr
Trade Group: US
Software Developer
Wages Fell 2% Last
Year
Wikileaks Releases
Docs Before Trial
of TPB Founder Warg
Article viewer

Replace PHP variables with custom error page (Apache)



Written by:MaxMouse
Published by:MaxMouse
Published on:2009-08-21 11:20:34
Topic:PHP
Search OSI about PHP.More articles by MaxMouse.
 viewed 6633 times send this article printer friendly

Digg this!
    Rate this article :
Recently i was trying to improve search engine spidering on a website, and read several articles regarding spiders, do they like something.php?somevar=foo&anotherVar=bar do they follow them using the entire querystring or not? well the jury is still out, there is evidence google (for example) does at least make a passing effort to spider them. But this wasn't good enough for me, the following article explains how to use custom 404 error pages to serve pages that do not exist.

I'm aware this could be done with mod_rewrite, however, each server i have used didn't have it installed, when i asked them to install it, i got told flat out "NO". So, i came up with another option, here it is.

Firstly you'll want to modify your .htaccess file (if it doesn't exist, create it), there are many options you can set within this file, what we want to put in there is:

ErrorDocument 404 /404.php


Note: Windows will not allow you to rename a file .htaccess, you'll have to open notepad and save it as "c:\foo\bar\.htaccess" - note the "'s you'll need to include them in the save file dialogue box.

This will instruct the web server to serve 404.php when a 404 Not Found occurs, now for the magic 404.php, I'll break it up and explain as we go:

<?php
$svr_proto = $_SERVER['SERVER_PROTOCOL'];

header($svr_proto . " 200 OK");
header("Status: 200 OK");


When a 404 Not Found occurs and 404.php is served, the headers still contain the 404 status code, search engines will think that nothing is there, and will not index the page. For this reason the above code changes the status code back to 200 OK.

$ruri = explode("_", str_replace(".html", "", str_replace("/", "", strtolower($_SERVER['REQUEST_URI']))));


The URI format used is http://www.somesite.tld/file_vars.html as you can see the above code removes the .html and the forward slash from the REQUEST_URI (which would contain /file_vars.html) and explodes it to an array using an underscore as a delimiter.

So http://www.somesite.tld/products_var.html would store "products" in $ruri[0] and "var" in $ruri[1] with some modification you could also deal with $_POST but currently i have no reason to implement that (The idea would be to grab the $_POST variables and put them inside variables too)

switch ($ruri[0]) {

 case "products":
  $from_404_pg = $ruri[1];
  include("products.php");
  
 break;
 
 case "gallery":
  $from_404_loc = $ruri[1];
  $from_404_img = $ruri[2];
  include("gallery.php");
  
 break;
 


The required file is then included which will also have access to the variables, which now reside in (using the gallery case as an example) $from_404_loc and $from_404_img, i could have simply used $ruri but the naming of the other variables makes more sense when you are working inside another file.

 
 default:
 header($svr_proto . " 404 Not Found");
 header("Status: 404 Not Found");
 include("true_404.php");
  
 break;
}

?>


The final piece of code (default switch case) is a TRUE 404 error, this means that none of the required conditions where met and now we must generate a real 404 Not Found, as you can see the headers are adjusted back to 404 Not Found and a true_404.php is included, this just displays a friendly error page, i will leave that up to you to write, you could even write code to read the URL from this and make suggestions as to the correct page, then make it send the webmaster an e-mail with details about a broken link.

Lets look inside products.php to see how we handle the variables passed from 404.php:

if (isset($from_404_pg)) {
$cpage = $from_404_pg;
} else {
$cpage = $_GET['pg'];
}


As you can see we still support the $_GET method (In my case, i have to support it still), but if the variable comes from 404.php we'll use that instead, now $cpage will contain the required variable: http://www.somesite.tld/products_var.html which you use as normal.

That's it even though http://www.somesite.tld/products_ice-cream.html doesn't exist on the server, the server makes it appear as a perfectly valid page (With valid HTTP Status code header) which will in turn look much more friendly to users and spiders alike.

I use the same method to create dynamic auto updating sitemaps and robots.txt files

Note: Remember the true_404.php can come from any error... so say for example you're true_404.php uses the image "images/error_page/whatever.jpg" if someone causes that error by going to http://www.somesite.tld/1/2/3/4/5/6/7/8/9/hello.html then you're true_404.php will try to load it's image from "1/2/3/4/5/6/7/8/9/images/error_page/whatever.jpg" obviously that doesn't exist... so all resources in your true_404.php file must be absolute IE: <img src="http://www.somesite.tld/images/error_page/whatever.jpg"> and if you're including php files, use the absolute path:

include($_SERVER['DOCUMENT_ROOT']) . "relative/path/to/your/file.php";

Did you like this article? There are hundreds more.

Comments:
<none>
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
elasolova
My PHP Projects on Sat 26th Sep 10am
I have been developing PHP applications for almost a year now. I have developed three projects. One is a simple trivia game. The other is a question-answer based community at http://www.javaist.com/quans . The last one is a programming challenge site just
countll
Blog entry for Thu 25th Oct 7am on Thu 25th Oct 7am
soo nu on this wicked world of NET. just decided to dive in today..hope friend aroun here can help

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
Test of experience (hopefully) by AcidIce

Things you're only likely to know if you've actually written a lot of PHP before :)


     
Your Ad Here
 
Copyright Open Source Institute, 2006