giza: Giza White Mage (Default)
If you run your own machine, you probably don't want to use Apache as a webserver. Its configuring is complex, arcane, and it sucks up memory like a Microsoft toaster. There are lighter, faster alternatives out there, such as Nginx or thttpd. But, if you buy webserver from someplace, you'll probably be forced into using Apache, and will be for the foreseeable future. That being said...

What is a rewrite rule?

A rewrite rule is a way that Apache can rewrite an incoming request "on the fly". In essence, a user can ask a webserver for one file, and the webserver can serve up a completely different file to the user instead.

Isn't this like a redirect?

No, a redirect is when the webserver tells a user, "that's not the file you want, you need to go over here", at which point the browser loads the new URL. Redirects aren't always desirable, especially of the files/PHP scripts/whatever are currently living a temporary location.

Why would I want to use one?

Let's say you're installing an app that somebody else wrote, and the app lives under /very-long-application-name. You could use a rewrite rule so that users could go to /short-app-name, and Apache would rewrite that request behind the user's back to be /very-long-application-name. And the best part is that this is transparent to a properly built application.

Another example is maybe you're starting up a video "tube" site, and you want to have the smallest embed code possible for sharing videos. Problem is, the video player lives at http://mysite/app/version1.0/video/player.swf. You could use a rewrite rule so that http://mysite/player.swf is rewriting to the longer URL. And what's even better is that when you release version 2.0 of the video player, you can just update the rewrite rule, and everybody will start seeing version 2.0 of your player.

Is this really used in real life?

Yep. The best example I can think of is Drupal. When you load a URL from a Drupal-powered site, such as http://drupal.org/drupal-6.6, that is really rewritten by Apache to be http://drupal.org/?q=drupal-6.6. Regardless which URL you load, Drual sees that the variable $q is really set to "drupal-6.6". It doesn't care which URL was used.

Okay, so how do I do it?

Ah, here's the good part. The first thing you need to do is put the following in your .htaccess file:
RewriteEngine On
If doing this gives you a "500 Server Error" when you try to load a webpage, go ask your web host to enable mod_rewrite. If they refuse, I would suggest moving to a webhost that doesn't suck.

Now, let's say you wanted to rewrite the path to your video player, as described in the example above:
RewriteRule ^player.swf$ /app/version/1.0/player.swf
Note the "^" and "$" in the "left hand side" of that rewrite rule. That tells Apache to match ONLY the string "player.swf", and not "/path/to/player.swf". This will prevent an infinite loop wherein that string is matched over and over. (Infinite loops are bad, m'kay?)

Most Drupal configurations have something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
That's some pretty advanced stuff there, and it uses a new directive, "RewriteCond". RewriteCond allows you to do 1 or more levels of matching in addition to the matching in the RewriteRule line. In this case, if the URI that the user attempts to load is neither a valid file nor a valid directory, then it is rewritten as index.php with the original URL passed in as $q.

I hope that this post was helpful, and saves you from going through some of the pain that I went through when learning how rewrite rules work. :-) If this wasn't enough pain for you, full documentation on Apache rewrite rules can be found on their official site at:

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Enjoy!

Profile

giza: Giza White Mage (Default)
Douglas Muth

April 2012

S M T W T F S
1234567
891011121314
15161718192021
22232425262728
2930     

Syndicate

RSS Atom

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags