The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

PHP URL Shortening

What's the best solution for shortening long url's that looks like form.php?state=ca&country=usa&area=bay_area ...
into
form.php/usa/ca/bay_area
or
form/usa/ca/bay_area
?

I've attempted to devise a method that doesn't require any packages. But, it involves dynamically creating directories with a script that parses all the get parameters into folders. I recently learned that there's a max number of directories and files allowed in a *nix directory, so this method is not extensible enough...

tons of directorys is the only solution i know of if you dont want to use a url rewriting solution like mod_rewrite. well, either that or make your 404 error page handle it, which i dont think is a good idea.

No, it does not create directories. It internally remaps URLs to different resources, often used to take long strings of directories (e.g. /form/usa/ca/bay_area) and remapping to a different resource with GET parameters (e.g. form.php?state=ca&country=usa&area=bay_area). Google "mod_rewrite tutorials" and I'm sure you'll have it up and running in no time.

good news! it turns out this mod_rewrite thing is installed with my cpanel!

i got a basic implementation to work, but ideally, i'd like to make these "virtual directories" dynamic.

this would mean using php to dynamically write the .htacccess file.

are there any security issues, here? precautions i should take? would it be safe for all users to access the same .htaccess file (in same parent directory), or should each user have her own physical directory and corresponding .htaccess file?

its not the best idea ot make your .htaccess files writable by php, since that will probably make them writable by all users on the server.

instead, consider either using some type of pattern/prefix to the urls you want rewritten, or use some type of catch all with exclusions.

exclusions would be something like
"rewrite all urls except a few select patterns, and/or some specifically named urls/dirs"
or
"rewrite all urls that do not actually map to a real, existing file or directory, or dont have a file extension etc..."

or some combination of the above components.

mod_rewrite is regex aware so it is extremely powerful.

directives set in .htaccess file cascade to directorys underneath it.
a pretty powerful and simple system can acheived by rewriting all urls by default, and then writing a few exclusions to match certain directorys you dont want rewriting to occur in. include a few other exclusions for stuff in your webroot like your robots.txt, favicon.ico etc...

once the request has been rewritten to your php script, then you can use php to decide what to do from there. for example, query the db to see if this "page" really exists.

sorry. suppose you see on your browser on a site you don't have serverside-access to. is there a way to tell whether a directory you see is being rerouted by mod_rewrite or if it's an actual physical directory?

also, is there a way for this to process spaces? using ([A-z0-9\s]) doesn't work...
example of why? how else would you process a query like script.php?name=Abe Lincoln?

these URL's have already gone into use, and I don't want to invalidate them unless there is a security issue with using this %20 instead of +

the mod_rewrite just redirects these and new virtual directories to script, which i can easily modify to accept +, but again, i would like the old directory infrastructure ---- with the %20's ---- to work.

question reposed for late-posters to thread: is there a way to make mod_rewrite allow for url-encoded spaces and alphabetical characters, such as "Abe Lincoln"

it's never decoded. the mysql table's name column expects names with spaces between first and last. (it is varchar, and existing data is in such form). the name is accepted with the space in mysql fetch.