Ok so you've read the
Basic Mod Rewrite Tutorial and want to go more advanced?
Here is a continuation by Mike from
http://www.webforgers.net.
Well here's the low down. Not many dynamic sites are going to stick to just a 1 variable query string. So how do we adjust our mod rewrite to pick this up.
For example lets say you had a page link this:
http://www.website.com/index.php?act=Post&CODE=00&f=3
I'll use this as our lab mouse
There are a number of ways we could rewrite this so lets see what we can come up with.
If we wanted to just seperate the values with dashes we could change the output to this.
http://www.website.com/Post-00-3.php
The code for our mod rewrite would be this
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)-(.*)-(.*)\.php$ /index.php?act=$1&CODE=$2&f=$3
To explain what all these symbols means I'll define them quickly
. = any single character
* = 0-infinite times ( it's a quantifier meaning it will look for the previous character 0 through an infinite amout of times.)
() = collect what is inside and store it as a variable
^ = start matching at the begining
$ = end matching
Now this is kind of a sloppy way to do it using
(.*) 2 times. We're just saying select what ever and be done with it. Any hard core programmer would chew you out, if all you used was the
.*. You'd be acused of using up server resources.
So let's break this down a bit. since we know that the variable for act= is going to be a word with no numbers we could replace the
(.*) to a character class that will look only for letters instead of any character+infinity. So we will change it to this
([a-zA-Z]+) instead of
(.*).
To explain like I did above what certain things mean:
[] = character class
+ = 1-infinite times ( it's a quantifier meaning it will look for the previous character 1 through an infinite amout of times.)
a-z = match any lowercase letter
A-Z match any uppercase letter
So
([a-zA-Z]+) would roughly mean match any letter upper and lower case 1 - infinite times and store it as a variable.
I hope you all are still with me. Cause now were going to make some character classes that look only for numbers.
The CODE= is going to be a numerical variable. So instead of
(.*) we can change this to
([0-9]+). Now instead of letters it will look for numbers. Still we can improve even more.
IF....big if...,
we know the amount of numbers or length of the number in the variable we can change it from looking for 1- infinite amount of times to a number that is x digits long. for instance if CODE=00 will always stay 2 digits long there is no need for it too look for a number like 5000 if the highest it will go is only 99.
To lighten the load on our server we can make our character class this
([0-9]{1,2}) now it will only look for a number that is atleast 1 character long and no less than 2 characters long.
Finally the last variable f=3
Simple just like the last variable we'll search for a number instead of using the .* to select ever character and it's mom.
This one though since it's only a single digit character we can modify it to just look for 1 character with out the atleast and up to quantifier. So this would simply be
([0-9]{1}) and we're done so our new RewriteRule would look like this.
Code:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z]+)-([0-9]{1,2})-([0-9]{1})\.php$ /index.php?act=$1&CODE=$2&f=$3
Of course you can always change the character that separates the variables. You can use
(_ , / , ~ , . ) or what have you. just make sure you change the -'s to the charater that seperates the variables.
Hope this helped