PHP
A discussion among other things directly and indirectly relating to the PHP scripting language
Tutorial: How to make friendly URL's in Apache - Part 2
@author Quad341.
This is part 2 of a 2 part tutorial using mod_rewrite in Apache*. If
you have not read the first part, I would suggest looking there for
more explanation, though not needed to use this.*Note: I say Apache because it is standard and everything written directly applies to Apache. There is a mod_rewrite for IIS that supports the exact same directives and a URL rewriter in LighTTPD that I have not personally used but you can see a comparison to Apache here.
2. Using PHP to handle the "redirect."
Now regular expressions are nice when you want to move parts around and add or remove other parts, but they don't to a whole lot of processing. PHP can do this processing and more. We can use PHP to do everything once the request in sent to it. All of the code presented will be written in a very poor way to show how it can be used. If you are using this method, I would suggest wrapping these in at least a function if not a full class.
First we need to set up our .htaccess file like the following:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.phpThis redirects everything to index.php except for request for pictures, javascript, and css. See the previous part for further explanation and modification instructions.
Now in our php file, we will do the file handling. First we need to get the actual url that was requested. That is stored in $_SERVER['REQUEST_URI'] and contains the full path. The easiest way to handle that string is to break it up on the front slash. That could look like the following:
<?php
$request_string = $_SERVER['REQUEST_URI'];
$request_parts = explode('/',$request_string);// process parts
?>This section will get all of the parts into a numerical array called $request_parts (remember, starts at zero). Now you could do a lot with this array. I would NOT suggest actually doing a redirect with this information. That is very poor looking for the user. Instead, the proper use of includes files or classes can help.
The Zend Framework does this processing in the Zend_Router class. It uses the following convention: /class/action/key/value/etc... which bascially means that you would neeed to load the class in $request_parts[0] and execute 1 from the same array. This is a very nice way to implement it.
You could do something more primative depending upon your requirements. You could merely keep all of your processing statements in other files to be included and have them set up to work with the array. Then you could have the first element as the include file name (actually, this should be a reference in an array so that one could not inject code or anything). You could even use array_shift to remove the first element from the array after using it.
This tutorial is unfortunately very abstract. The processing potential in PHP makes these choices very dependant upon what you wish to accomplish. Below is an example of the "primitive" method described above.
<?php
$request_string = $_SERVER['REQUEST_URI'];
$request_parts = explode('/',$request_string);
// array to hold the mappings. the key is placed in the url. the value is the file name.
$map = array('index' => 'index.inc.php','news'=>'news.inc.php',
'users'=>'users.inc.php',
'DEFAULT'=>'index.inc.php');
$inc_key = array_shift($request_parts);
if(array_key_exists($inc_key,$map))
include($map[$inc_key]);
else
include($map['DEFAULT']);
?>All of the processing of the files takes place in the includes files. This also prevents people from injecting any code or other files into the list as well as provides easy methods to update and change pointers of the processing files without updating the links.
This was meant to be a concise tutorial. Please leave comments as to how to improve!
Comments: http://www.zimbio.com/portal/PHP/forum/5
From Our Partners
People in Pictures
Top Geek Articles
|
Celebrities on the Phone
Cell phones are to celebrities like bats are to baseball: no one runs too far without them.
|
|
Why every guy should buy their girlfriend Wii Fit.
Gratuitous...
|
|
Hot Geeks -- The Sexiest Geeky Girls
These girls are gorgeous AND they'll play Warcraft with you. Doesn't get much better than that.
|



