开发者

PHP: Parse different styles of friendly urls

开发者 https://www.devze.com 2022-12-16 20:31 出处:网络
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:

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 cor开发者_JAVA技巧rectl handling URLs UTF-8 encoded or with magic_quotes_runtime etc...


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消