0

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...

Vladislav Rastrusny
  • 29,378
  • 23
  • 95
  • 156
  • Why do you want to keep the old system? If someone has bookmarked a URL with the old system, and it rewrites to the new system, they will still get to the right site, even if the URL changes to the new system. – Anthony Jan 13 '10 at 20:15
  • Yes, that is correct. What do you mean "keep old system"? I need all types of the links to work at the same time. – Vladislav Rastrusny Jan 13 '10 at 20:20
  • If you want all of those links to go to the same place, just have all requests that contain "showprofile" rewrite to a script that then outputs the profile for the username. – Anthony Jan 13 '10 at 20:35
  • The first URL is simple `$_GET` variables. The second and third you could simply split on the `?` or `/` characters to get the parameters. The fourth you could just rip from a framework; it's just `.htaccess`. What I'm trying to say here is you could do each of these in 5 lines of code or less. There aren't any libraries around for this because it's trivial to implement. – ryeguy Jan 14 '10 at 15:17

3 Answers3

1

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
  • 40,604
  • 9
  • 72
  • 101
0

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
  • 776,304
  • 153
  • 1,341
  • 1,358
0

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
  • 70,219
  • 68
  • 205
  • 290
  • 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 – Vladislav Rastrusny Jan 15 '10 at 12:17