Webmaster Forums 101 - Free Help and Discussions
 
 
 

MEMBER REWARDS - Earn free advertising by participating in these forums! Click here for details.

Welcome to Webmaster Forums 101!

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

Reply
 
Thread Tools Search this Thread Display Modes
Old 17th January 2005, 03:54 PM   #1
Mr. Tech's Avatar

Username: Mr. Tech
Rank: Problem Solver
Join Date: Dec 2004
Location: Gold Coast, Australia
Posts: 2,166
Points: 1,025.61
Send a message via MSN to Mr. Tech
Post Advanced Mod Rewrite: Fake Filesd

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
__________________
Please read the Forum Rules

Get rewarded for posting
- free advertising!

.::. [ Webmaster Resources ] .::. [ Web Design ] .::. [ Developer Tutorials ] .::.
Mr. Tech is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mod Rewrite and Sub Folders Ben Website Design & Coding 11 23rd February 2005 10:21 AM
Basic Mod Rewrite: Fake Files Mr. Tech Tutorials 0 17th January 2005 03:38 PM

Login
User Name
Password


Featured Members


Featured Links


Useful Resources
Webmaster Resources
Developers Tools
Developer Tutorials


Partners

All times are GMT +10. The time now is 05:07 AM.


Powered by vBulletin Version 3.5.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.3.2
 
 
WEBMASTER FORUMS 101
ARCHIVE
CONTACT US
TOP
   
© Webmaster Forums 101 2005. All Rights Reserved.
Design by: vBCore