rectanglicle

the personal website of ash m. white

06
Sep
2008

Maintaining Clean URLs With mod_rewrite

Filed under: How-To, Apache

Tagged: apache, httpd, mod_rewrite

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

	# This line may or may not be necessary, depending on your server environment. 
	# The path is a URL, relative to your site's URL.
	# RewriteBase /

	RewriteCond %{REQUEST_FILENAME} -f [OR]
	RewriteCond %{REQUEST_FILENAME} -d [OR]
	RewriteCond %{REQUEST_FILENAME} -l
	RewriteRule ^(.+) - [PT,L]

	# "Down for maintainance" page. Uncomment this to turn site access off.
	# RewriteRule ^(.*)$ maintenance.html [L]

	RewriteRule !.(js|ico|gif|jpg|css)$ index.php [L]
</IfModule>

Comments

There are no comments yet

Leave a Comment

I am a software developer and designer in Athens, Georgia. I'm passionate about web development, graphic design, and web standards. I am currently enrolled in the University of Georgia Master of Internet Technology program. More...

@ashwhite

Dear @charlesproxy, You're amazing. Love, Ash

about 15 hours ago

Thanks to @johnallsopp for the very inspiring talk tonight about HTML5 and the future of web technology.

about 3 days ago

@Stormink Other bad changes aside, CDs are not analog. If the icon had started as a vinyl record, that would be a totally different story.

about 3 days ago

@koefod You're on a Mac, right? You should give this a try: http://agilewebsolutions.com/products/1Password

about 3 days ago

When method names include the word "kludge," it's a bad sign.

about 4 days ago