PHP: managing mixed URLs in a CMS

This is something that can actually drive you mad (and it's actually driving me mad) if you tend to neglect it. Basically, a CMS can handle two types of URLs: static and dynamic: A static URL looks like http://example.com/page/title, while a dynamic one it's something like http://example.com/?page=title. First of all, it's a pure myth that Google or other search engines have problem with indexing a dynamic URL (see Google's blog if you don't believe it), because spiders follow links and, just because they can handle whatever GET request comes on their way, they can handle dynamic URLs pretty well. So this is not a point for choosing a static URL. By modifying the default server configuration (for example, through an .htaccess file), you can redirect all requests to a single PHP file that will manage your entire content. The fact is, however, that if you choose to use both dynamic and static URLs you will probably end up with facing a lot of problems when managing complex URLs.

Many innocent developers think that a GET request such as ?page=title&status=live can be handled simply thorugh the $_GET superglobal array. Actually, this associative array can handle only one variable at once, not multiple variables. So we're forced to use the value QUERY_STRING of the $_SERVER superglobal array. But even this is not easy, because we should write a very generic function that transforms the resulting string into an associative array by separating name= from value. Imagine what might happen if you have complex URLs such as Facebook's ones! In this case, the best thing to do is to rely on a PHP framework (such as Zend) that does the dirty work for us.

The same advice applies to static URLs. Sure, you can parse static URLs through PCRE (Perl Compatible Regular Expressions) if they come from a request, of through parse_url if they're embedded in a string. But what happens for long URLs? Total madness! Many people don't like frameworks because they're not accustomed with their rules, but I think that sometimes we could make an exception, right?

Leave a Reply

Note: Only a member of this blog may post a comment.