views:

250

answers:

3

Hello.

I am writting my own small framework and I am going to implement friendly URLs there. mod_rewrite is great. But I want to be able to handle several types of friendly URLS at once:

/index.php?ac=user&an=showprofile (fallback variant, the worst)
/index.php/user/showprofile (supposedly, can be disabled by security settings)
index.php?user/showprofile (optional, not needed)
/user/showprofile (ideal, but requires mod_rewrite or dirty ErrorDocument tricks)

I would like all the variants to be supported at once so that old links generated with whatever scheme would be forever valid. Should I write my own parse functions for this or, may be, I missed some library/script, that can do that? Extracting algos from big frameworks like Symfony or Zend is quite difficult. There are also many different unobvious cases like correctl handling URLs UTF-8 encoded or with magic_quotes_runtime etc...

A: 

The first through third should be passed through to PHP for processing ($_GET, $_SERVER["REQUEST_URI"] and $_SERVER["QUERY_STRING"]).

The fourth can be done with mod_rewrite using RewriteCond !-f and an appropriate RewriteRule.

Ignacio Vazquez-Abrams
I know how it can be done. I just don't want to reinvent the wheel if somewhere already exists a library for doing this.
FractalizeR
+1  A: 

If you can both programmatically distinguish between all different types of URLs and normalize them to one base form you can just write a simple tokenizer function that normalizes the different types and you can use the normalized type to get the actual destination. I've done this, but not without mod_rewrite. Pretty sure it can be done without it though.

I usually have one index file that parses whatever url and then does a bunch of request handling and routing to get the output, without having any url map directly to any file. Just mod_rewrite everything to that index file and parse $_SERVER['REQUEST_URI'].

Kris
A: 

You should avoid allowing all different types of URLs. If all those URLs you posted show the same content then SEs will see it as duplicate content and you will end up "splitting" your ranking. In other words, each of the four pages will rank a quarter as well as if everything was focused on one URL...if that makes sense.

Just choose one URL format that's simple for users, and stick with it. Personally I would prefer the last one, with mod_rewrite. Your question implies there is something "wrong" with using mod_rewrite, which there isn't.

You can also use things like $_SERVER["REQUEST_URI"] to detect what URL is being requested and do a 301 redirect to the official URL, if that's even necessary.

DisgruntledGoat
Of course, one site will stick to one type of links. To prevent content splitting, you can send HTTP 301 code on one link type. This is a fallback mechanism to handle both links at once. As an example look at VB4 implementation: http://www.vbulletin.com/forum/entry.php?2399-Friendly-URLs-and-Character-Encoding
FractalizeR