Athens Exchange
  • home
  • daily
  • athens
  • music
  • film & tv
  • food
  • sports
  • sci & tech
  • popfest 2008
 
Saturday, March 13, 2010
Weather: , °
search:  
Post a Comment        E-mail this Article to a Friend        Join the List        AddThis Social Bookmark Button

Coder's Corner: Maintaining Clean URLs With mod_rewrite

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

<IfModule mod_rewrite.c>

Options +FollowSymLinks

RewriteEngine On

# RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]

RewriteCond %{REQUEST_FILENAME} -d [OR]

RewriteCond %{REQUEST_FILENAME} -l

RewriteRule ^(.+) - [PT,L]

RewriteRule ^.*(includes.*)$ $1 [PT,L]

 

RewriteRule ^([^/]*)$ index.php?args[]=$1 [L,QSA]

RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*) index.php?args[]=$1&args[]=$2&args[]=$3&args[]=$4 [L,QSA]

RewriteRule ^([^/]*)/([^/]*)/([^/]*) index.php?args[]=$1&args[]=$2&args[]=$3 [L,QSA]

RewriteRule ^([^/]*)/([^/]*) index.php?args[]=$1&args[]=$2 [L,QSA]

</IfModule>

Comments   [post a comment]

Comments are closed

  • More Technology
    Social Bookmarking: It's pretty del.icio.us!
    by Allison Brill
    10.12.2008

    Cold, Hard Chrome: Google Just Got a Little Cooler
    by Allison Brill
    09.02.2008

    Coder's Corner: Maintaining Clean URLs With mod_rewrite
    by Ash White
    05.10.2008

  • More From Ash White
  • [Recorded] What Transition? Moving From PowerPC Mac To Intel Core 2 Duo Mac
  • [Recorded] Rhabdophobic, Just Passin' Through


Contact • Contribute • Privacy Policy

© 2010 Athens Exchange
Powered By Boxkite Media