How to make my PHP-generated URLs prettier?
October 16, 2009 12:55 PM   Subscribe

I'd like to make more easily understood URL paths for my PHP-generated pages.

Instead of using the conventional query string method such as

mysite.com/page.php?section=2&product_id=1

I'd like to be able to set up my URL to read

mysite.com/tools/widget/

It's more human-readable, and I think better for SEO, but I don't know how to do it! Unfortunately, all my Google and PHP.net searches around URL parsing, etc. don't seem to address how to use alternatives to the ?& method of using GET variables. Does anyone have any advice or links to articles on this subject?
posted by fellorwaspushed to Computers & Internet (9 answers total) 2 users marked this as a favorite
 
If you're using Apache, mod_rewrite is your friend.
posted by usonian at 12:57 PM on October 16, 2009 [1 favorite]


You need to be searching for url rewriting. Take a look at the wikipedia page for rewrite engine. Some advice on mod_rewrite: I struggled with mod_rewrite for a couple of days to figure out get nicer looking urls. I'm not sure if my understanding of it is correct, but what seems to be happening is that mod_rewrite uses regular expressions to capture the values in a nice url and then sends a request using those values in a query string. So it was backwards from my first conception of url rewriting... mod_rewrite wasn't taking the messy query and showing me the nice url, it was taking the nice url and making it into the query string a PHP script would normally use for GET variables. So you don't have to really change what your scripts are doing so much as what your links are and adding rewrite directives to an .htaccess file to process them.
posted by Mister Cheese at 1:26 PM on October 16, 2009


This is the tutorial I used to first learn mod_rewrite, but I had to supplement it a bit with some information about regular expressions.
posted by Mister Cheese at 1:31 PM on October 16, 2009


This article is what I used a few years back.
posted by backwards guitar at 1:48 PM on October 16, 2009


You also might google around about the Front Controller pattern which is maybe more complicated than you want, but is the same sort of thing.
posted by toomuchpete at 1:49 PM on October 16, 2009


Other people have beaten me to the mod_rewrite thing, but I think it should be clarified a bit:

mod_rewrite is not a PHP thing. It's a webserver thing.

You will NOT be using PHP to "parse URLs"... Instead, your web server (Apache, hopefully, otherwise you need a mod_rewrite alternative) will be parsing them, and converting matches to GET variables if you have it set up to do so for that particular path.

Be careful with this, however. You will hamstring yourself if you use something like translating:

mysite.com/*/* to mysite.com?$1=$2 ($1 just means "the first token match" or the first * you see in this particular example)...

Instead, parse for SPECIFIC strings...

A real-world example:

I want mysite.com/foo/bar to actually go to a page in /foo/bar ...

However:
I want mysite.com/news to show listings of news...
I want mysite.com/news/page/2 to show the second page of listings...
I want mysite.com/news/entry/$entryNumber to show that entry.
I want mysite.com/news/somethingfake to actually try to go to /news/somethingfake instead of screwing up...

I would do something like:
# This detects the section of my site someone's going to...
RewriteRule ^(news|media|whatever)(/)?$ index.php?section=$1
# This detects if someone's trying to look at a sub-page of my news section
RewriteRule ^news/page/([a-z0-9]*)$ index.php?section=news&page=$1
# This detects /news/entry/123 -- or with an optional SEO friendly slug, i.e. /news/entry/123/this-is-a-slug
RewriteRule ^news/entry/([0-9]+)(/([\w\-]*))?$ index.php?section=news&entry=$1

posted by twiggy at 1:50 PM on October 16, 2009


mod_rewrite has always struck me as the messy and difficult way to go about this. I think it's easier to skip the query-string stage entirely and use the path info via the $_SERVER variable. Here's a tiny example (apologies if weird; I haven't used php in a while). Drop that somewhere as foo.php, and then look at /foo, /foo/bar, /foo/bar/baz, etc.
posted by hattifattener at 2:18 PM on October 16, 2009


Response by poster: Thanks for the tips, but unfortunately the project in question is on a shared Windows server, so it doesn't seem like .htaccess is an option, nor is compelling my client to move to Apache. Any ideas how I can implement an equivalent solution for IIS? The server uses a Plesk admin interface, if that's of any use.
posted by fellorwaspushed at 11:57 AM on October 18, 2009


there is a URL Rewrite Module for IIS. I can't provide any more details, since I've never used IIS.
posted by jrishel at 8:37 AM on October 19, 2009


« Older FatCow or NearlyFreeSpeech?   |   Wildflowers near Los Angeles? Newer »
This thread is closed to new comments.