Coder's Corner: Maintaining Clean URLs With mod_rewriteby Ash White 05.10.2008 The problem? You've seen it before, and you probably hate it as much as I do. You know... when the URL of the site you're visiting becomes so long it doesn't even fit into the viewable space of the URL bar. Okay, you may not hate it as much as me, or maybe you never even noticed it at all. But I'll bet you'll notice it from now on. The culprit? Query strings. In English, that means everything after the "?" character in the URL. They originate from the previous page, telling the current page which actions to take, how to take them, and provide data values relevant to the current page. The most common example would be seeing all of the values you typed into an HTML form show up on the form's target page. The answer? Apache has a module called mod_rewrite that comes with most base installations of the web server. This wonderful little gem of a module lets you transform URLs anyway you'd like behind the scenes. For instance, you can take all query variables and turn them into folders, so /index.php?page=article&article_id=1 would turn into something like: /article/1 Rather than just being more aesthetically pleasing, the latter is much easier for your visitors to remember. How? Plop this into a .htaccess file in your site root (you'll need to tell Apache to AllowOverride for that directory). The result will be that all request variables (this example only does the first 4) will be sent into an array residing inside the $_REQUEST array. So on the PHP side, you would access the request variables by using $_REQUEST['args'][0], etc.
Comments are closed |
|
Comments [post a comment]