Passing from 1 to 3 variables to my script

Discuss practical ways rearrange URLs using mod_rewrite.

Passing from 1 to 3 variables to my script

Postby seven[hr] » Mon Mar 21, 2005 3:32 am

Hi,

Please, help me with my logic of mod_rewrite usage. I really don't have so much expirience like some of guys in here but willing to learn. :)

I am trying to write general .htaccess mod_rewrite rules to use it on most of my production sites. I am using rather trivial url variable method to generate content pages trough my sites.

eg:

1. index.php?content=something [calls general list]
2. index.php?content=something&action=view&id=34 [shows entry]
3. index.php?content=something&action=send [eg. submtes new entry]

I wan't to rewrite im like this:
somesite.com/something/
somesite.com/something/view/34/
somesite.com/something/send/

Now... I am not shure should I CONSTANTLY rewrite both 3 variables and pass it to my script (like this):

Code: Select all
RewriteRule ^(.*)/(.*)/(.*)/$ index.php?content=$1&action=$2&id=$3

... OR I should have Rewrite Conditions for NUMBER of variables sent, and then redirection to link with one or two or three variables sticked to it:

1. if there is only one parametar - www.pero.com/bla/
2. rewrite to script and give just one parametar -
Code: Select all
RewriteCond %{REQUEST_URI} ^(.*)/$
RewriteRule ^(.*)/$ index.php?content=$1

RewriteCond %{REQUEST_URI} ^(.*)/(.*)/$
RewriteRule ^(.*)/(.*)/$ index.php?content=$1&action=$2

RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/$
RewriteRule ^(.*)/(.*)/(.*)/$ index.php?content=$1&action=$2&id=$3


Anyways, my RewriteCond / RewriteRule rules are not working. I get so strange results with 3 variables passed.

Please help me out.

Thank you in advance
7even
seven[hr]
 
Posts: 2
Joined: Mon Mar 21, 2005 3:20 am

Postby seven[hr] » Mon Mar 21, 2005 4:10 am

I could also do this:

fetch all /bla/bla/bla variables from string and pass it to my script.
Code: Select all
RewriteRule  ^(.*)/$   index.php?vars=$1 [L]



Script would make an ARRAY of variables:
Code: Select all
$vardata = explode('/', $vars);



What is more logical thing? Should I harrest mod_rewrite or I should place url rewrite logic back to php?
seven[hr]
 
Posts: 2
Joined: Mon Mar 21, 2005 3:20 am

Postby Caterham » Mon Mar 21, 2005 6:17 pm

a few points: You should avoid the regEx .* or .+ -- they will match any character and are slow.

The '?' makes the char left to it optional, in this case it's the slash. This are all Regular Expressions and nothing special only for mod_rewrite.


Code: Select all
RewriteRule ^([a-z0-9_-]+)/?([a-z0-9_-]*)/?([a-z0-9_-]*)/?$ index.php?content=$1&action=$2&id=$3 [L]

Important to know: variables might be empty like index.php?content=VALUE&action=&id= if example.com/VALUE was requested.
__________________________

RewriteCond %{REQUEST_URI} ^(.*)/$
RewriteRule ^(.*)/$ index.php?content=$1

RewriteCond %{REQUEST_URI} ^(.*)/(.*)/$
RewriteRule ^(.*)/(.*)/$ index.php?content=$1&action=$2

RewriteCond %{REQUEST_URI} ^(.*)/(.*)/(.*)/$
RewriteRule ^(.*)/(.*)/(.*)/$ index.php?content=$1&action=$2&id=$3

The Conditions are redundant. The Pattern of the rewriteRule is processed before the condition would apply. They are not working, because you're using the greedy RegEx .*. --- .* means any char, so the pattern of your 1st rule,
RewriteRule ^(.*)/$
would also match /value1/value2/value3/value4/value5/ etc. The RegEx
[a-z0-9_-]+
does not allow to match the slash, so you can use
RewriteRule ^([a-z0-9_-]+)/$ index.php?content=$1 [L]
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/$ index.php?content=$1&action=$2 [L]
RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9_-]+)/$ index.php?content=$1&action=$2&id=$3 [L]
The L-Flag will force to exit further processing of other rules below if a rule did match.

you might want to add some more chars to the character class like
[a-zA-Z0-9_.-]+
make sure the hyphen '-' is at the end, otherwise you'll need to escape it like
[a-zA-Z0-9\-_.]+

__________________________

I could also do this:

fetch all /bla/bla/bla variables from string and pass it to my script.
Yes, you'll have to do so if you have more than 9 variables, because you can only create backreferences from $1, $2.....$9
What is more logical thing? Should I harrest mod_rewrite or I should place url rewrite logic back to php?
you are using mod_rewrite here, so you can easily pass the variables already to the queryString. [a-z0-9_-] is not only faster than .* but it checks also allowed characters, some additional security for your script. If you need nearly all chars, it might be more efficient to specify chars which are not allowed to match:
# match 1 to n chars except the slash
[^/]+



1. if there is only one parametar - www.pero.com/bla/
You're linking on a german website here; -> http://www.modrewrite.de
Caterham
 
Posts: 690
Joined: Fri Dec 10, 2004 1:30 pm

Postby Guest » Tue Mar 22, 2005 2:19 am

Thank you a lot mate! Things are much clearer to me now.

I am a sad exceuse for programmer who never used regular expressions. :)

I will studdy modrewrite.de more closely. :)

Best regards
neven
Guest
 


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 28 guests

cron