by
Miran Lipovaca aka foodpk | 7 April 2007In the
previous
page, you learned about the advantages of clean URLs,
and we just got started explaining what the code you copied
and pasted was. In this page, let's continue by learning
about rewrite rules.
OK, so now comes the rewrite rule. A rewrite rule is
comprised of a regular
expression (the request is matched against it) and if it
matches, it gives the
client the file specified afterwards. Now I can't go about
explaining regular
expressions as a whole, for that's a pretty hefty subject, but
we'll take a glance at
what's done here.
If we take a look at the first rewrite
rule:
RewriteRule ^([a-zA-Z0-9\-]+)/?$
/index.php?category=$1 [L,QSA]
it tells the server
to match any character from a-z, A-Z, 0-9 and the dash
-
character. The + means
it matches either 1 or many of those characters. Then
there's a dash / and a
question mark. A question mark after the dash means 'match
either one or no dash
at all'. The $ means the end of the line.
So the first regular expression basically says: "If something
that's comprised of
letters, numbers and the dash line, catch it. It doesn't
matter if it has a slash afterwards or not."
Whatever we put in the parenthesis, we can use as a
variable. After the regular
expression, we reach that variable by using
$1, or
$2 if
it's the second variable,
and so on.
As you can now probably see, the first rewrite rule turns,
say, http://yoursite.com/index.php ?category=cooking into
http://yoursite.com/cooking (or
http://yoursite.com/cooking/)
Pretty, sweet, huh?
The second and third rule do the same thing, except they
come to effect when someone
requests two 'directories', like
http://yoursite.com/cooking/pasta. Then it gives
that person
http://yoursite.com/index.php?section=cooking&page=pasta
In the example I've included the ability to read three 'fake
directories' (so you
can do
http://yoursite.com/one/two /three), but if you need
more, just add another
rewrite rule, I'm sure you can see what the pattern used is.
The QSA
in the [L,QSA] means that
you can append query strings to the URL.
So
http://yoursite.com/cooking/
pasta/4/?show=something will work.
Below is a simple example of how to put all this mod
rewriting to work. First put
the above .htaccess in the root folder. Then, use the
following code as your index.php:
Let's say the user requests
http://yoursite.com/aboutme. First the .htaccess will
interpret that and tell the server to call
http://yoursite.com/index.php ?category=aboutme.
Then the index.php will set $file
to aboutme.php and check if it exists. If it exists, it will
include aboutme.php
into the page. If it doesn't, it will tell the user that tha
page wasn't found.
The above was just a simple example, you can use what we
learned here to get many
more variables (like
http://yoursite.com/one/two /three) and then include
them
however you want. Most of the time you will be including
some of them while using
others to get stuff out of a database according to them.
One final note, if you have problems with links and images
becoming broken after
using this, you should put a base tag in the head of
your (X)HTML document, like so:
<base href="http://yoursite.com/"
/>
The base tag tells the browser where to begin looking when
relative paths are in play.
If you have any questions regarding mod rewriting, feel free
to ask them in the
forums!
High five!
|