[UPHPU] URL Rewriting

Mac Newbold mac at macnewbold.com
Mon Apr 28 15:38:44 MDT 2008


Apr 22 at 10:11am, Richard K Miller said:

> I'm not sure you could do *any* URL rewriting without at least a bare minimum 
> of mod_rewrite.

Sorry to resurrect a thread that ended last week, but I wanted to bring up 
a method that takes a different approach to it. One benefit, though I 
don't recommend it, is that it isn't Apache dependent. Even IIS can do it, 
though it is limited in some ways.

One way to perform an equivalent function (allow nice looking URLs that 
don't correspond to files Apache could serve directly) without using 
mod_rewrite is to specify a PHP script as your ErrorDocument in your 
web server config or .htaccess. (Any web server that will let you choose 
to run a PHP script on error instead of sending back a 404 and an html 
page can do this.)

For example, put in your .htaccess or apache config:

ErrorDocument 404 /error.php

When there's an error, your error.php script will run, and can examine the 
URL they requested in $_SERVER[REQUEST_URI] . Then you can write PHP to 
determine what and how you want to handle it, and what status code you 
want to send back. To give some ideas of how one might use this:

1. Based on URL, redirect with header("Location:") to the rewritten URL
2. Decide which page they meant, and include or require it
3. Like Cake and Wordpress do, parse the URL into some variables that mean
    something to you, and render the resulting page accordingly
4. Look up the URL in a database that gives you real file names, then send
    back the right file
5. Run an automatic search, like http://php.net/function_name does
5. If you're really confused, send back a custom Error page explaining
    their mistake

One advantage is you don't have to learn mod_rewrite syntax or depend on 
it being available on the server in question. You can also do anything 
that PHP can do as you decide how to transform the URL, so there are a lot 
more possibilities than mod_rewrite alone can offer.

Thanks,
Mac

--
Mac Newbold		MNE - Mac Newbold Enterprises, LLC
mac at macnewbold.com	http://www.macnewbold.com/


More information about the UPHPU mailing list