Lab - 404 PHP handler
Implement the
404 PHP handler
- Get .htaccess working
- Write 404.php
Simple version
- Find the bad URL:
- 404.php extracts the bad URL using
REDIRECT_URL
in
$_SERVER
- Find the RHS part of the bad URL:
- Split the bad URL into segments using
explode
on "/".
This makes an array of segments.
- Simple version: Assume the bad URL was "/~user/file" and just extract the RHS "file" bit.
- Make a list of good URLs:
- Make a pre-built list of all good URLs.
Put this list in the file filelist.
- Case-insensitive and partial search:
- Search the segment in the list of good URLs, using a
command-line call
to something like:
grep -i segment filelist
- This outputs the lines that match.
- Make the hit linkable:
More complex version
- Sanitise all user supplied data, including user supplied URL.
- Allow more complex bad URL: "/~user/dir/dir/file"
- "explode" gives us an array of all the segments
- We look at the RHS segments first. So reverse the array.
There is a PHP function to do this.
- Then go through the array with foreach.
- Search each segment. Collect the return code from grep.
- Exit when we got a match.
- Assume each grep could have multiple matches
- Split the return into an array
- Print possibly multiple links