Mailing List Archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[tlug] [OT] Creating user-friendly URLS with mod_rewrite/Apache



TLUG,

My apologies if this strays too far off topic. This is basically an Apache related question, which I'm hoping falls under the category of "Linux server" issues enough to be acceptable here. I have a suspicion that this is an area the TLUG members would be very knowledgeable on. But do redirect me if this thread is inappropriate.

As I originally asked on the PHP general mailing list, I'm working on how to get nice, user friendly, and web search engine friendly URLs. This is part of a huge overhaul of some web sites I work on to be more standards compliant, user friendly, and logically structured.

In other words, change from this "ugly" URL:
tokyocomedy.com/people.php?person=247

To this "clean" URL:
tokyocomedy.com/kevin_cooney

I've checked some tutorials and references, which I'll link to below.

It soon became fairly clear that the issue is not so much about how PHP handles information passed to it from the URL, the issue is making sure that the URL passed from the browser to the server goes to the right script. Once I've got my URL passed to the right script, I'm all good. But making Apache play nice is where I'm out of my depth.

In the tutorials I looked at, it showed how I could take the above "ugly" URL, and change it to:
tokyocomedy.com/people/kevin_cooney

This is done by making a PHP script called "people" without the .php extension, and then in the .htaccess file, specifying:

<Files ~ "people">
ForceType application/x-httpd-php
</Files>

Not bad. But for reasons of design philosophy, I wanted to take it a step further.

You see, I think things like having a "people" category is of far more convenience and interest to web programmers than it is to web site visitors. To a user, of course "Kevin Cooney" is a person (well, if you met him you might wonder), and specifying so is redundant.

Don't get me wrong, I'm not against categorization. But in this instance, and for most of the web sites I do, it's clearly redundant.

So what I really want is for Apache and PHP to play nice together so that it will take pretty much anything after the ".com", in the above example where it says "kevin_cooney", and pass that to index.php to be used for looking up data in the database and passing the right information.

But wait, there's another issue here. On my "tokyocomedy.com" web site, there is an actual subdirectory called "japanese", which is all about the Japanese side of the comedy group, and all the HTML in there is maintained by a different dude than me. So I'd want to make sure that certain exceptions will go to the right directory.

In the end, I think there are four cases:

1. The user types in the basic URL only, "tokyocomedy.com", which should resolve to index.php and display the home page. (This is actually probably just a subset of case 3, but I thought I'd mention it anyway, just in case).

2. The user types in the name of a real existing directory, like "tokyocomedy.com/japanese", which should actually go to that directory.

3. The user types in something that refers to database driven data, like tokocomedy.com/kevin_cooney", and it gets passed to index.php (or something) that will parse the URL and handle it.

4. The user types in something else, maybe even some hacking attempt, in which case I want radioactive lizards to be shot out of the screen and bite them in the neck. Or maybe just "404", or simple redirect to the home page.

The tricky part seems to be getting Apache to work without at least some kind of "category" directory. After all, without that, what would I specify in ".htaccess" in order to tell Apache where to take the link?

A helpful person on the PHP list said I needed to look into mod_rewrite. So I looked it up, and whoah - complicated. By their own documentation, it's sendmail-level complicated, which means it was written by diabolical mad scientists from the future with the intent to drive people insane.

And that's when I decided to turn here, in hopes of getting a little clarity.

Can anyone shed a little insight into what I might do here?

Any advice or information will be greatly appreciated.

Here are some of the references I'm using:
http://agachi.name/weblog/archives/2005/01/30/rewriting-dynamic-urls-into-friendly-urls.htm
http://richardlynch.blogspot.com/
http://meta.wikimedia.org/wiki/Eliminating_index.php_from_the_url

--
Dave M G



Home | Main Index | Thread Index

Home Page Mailing List Linux and Japan TLUG Members Links