Welcome to WebmasterWorld Guest from 23.20.147.6

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Variable in htaccess

RewriteRule question

     
8:37 am on Jul 9, 2010 (gmt 0)

New User

5+ Year Member

joined:July 9, 2010
posts:1
votes: 0


Hello, how could i use a RewriteRule accesing a variable?


RewriteRule ^(skoda|bmw|mercedes)-([0-9]+)-([0-9]+)-some-think$ index.php?a=$1 [L,QSA]

RewriteRule ^some-one-(skoda|bmw|mercedes)/pag-([0-9]+)$ index.php?a=$1 [L,QSA]

RewriteRule ^a-z-(skoda|bmw|mercedes)$ index.php?a=$1 [L,QSA]


(forget second part of RewriteRule) .. I don-t want to put everywhere (skoda|bmw|mercedes). Is more quickly to make a variable then to use it in rule... because when i want to edit it to add more options .. to not search in all of my rules for it..

So my question is, how could i set an env variable then to use it in all my rules where i need it, like:


SetEnv MY_VAR (a|b|c)

RewriteRule ^%some-one-{ENV:MY_VAR}$ index.php?s=$1 [L,QSA]

RewriteRule ^%{ENV:MY_VAR}-some-one$ index.php?s=$1 [L,QSA]
5:42 pm on July 10, 2010 (gmt 0)

Senior Member

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Mar 31, 2002
posts:25430
votes: 0


Mod_rewrite cannot use a variable in a regex pattern. The .htaccess directives are not a scripting language...

I'd recommend:

RewriteCond $1<>$3 ^<>-([0-9]+)-([0-9]+)-some-think$ [OR]
RewriteCond $1<>$3 ^some-one-<>/pag-([0-9]+)$ [OR]
RewriteCond $1<>$3 ^a-z+-<>$
RewriteRule ^([^\-/]+[\-/])*(skoda|bmw|mercedes)([\-/].+)?$ index.php?a=$2 [QSA,L]

Here, the RewriteRule pattern is evaluated first (See Apache mod_rewrite documentation "Rule Processing").

If the pattern matches, then whatever comes before "(skoda|bmw|mercedes)" in the requested URL-path is placed into local variable "$1".

Whatever follows "(skoda|bmw|mercedes)" is placed into local variable $3.

The value of the requested URL-path matching "(skoda|bmw|mercedes)" is placed into $2.

Then each of the RewriteConds is processed to check that the format of the requested URL without the "(skoda|bmw|mercedes)" part is one of the formats to be accepted.

Note that the "<>" characters are used only as a separator to assist correct and unambiguous parsing, and have no special meaning as used here. They simply "take the place of" the variable-string that you do not want to include in each line. You can use any character or characters that you are sure will never appear in one of your URLs without first being URL-encoded. I prefer to use any of > or < or ~ myself.

Note also that the RewriteRule assumes that the "(skoda|bmw|mercedes)" substring will always be delimited by either a hyphen or a slash if any other substring precedes or follows it. I am referring to the two RewriteRule sub-patterns containing "[^\-/]" ("NOT a hyphen or a slash") and "[\-/]" ("Match a hyphen or a slash"). This greatly improves efficiency of regular-expressions pattern matching, so use this method if possible instead of using an ambiguous and inefficient sub-pattern like ".*" (Match anything, everything, or nothing").

Jim
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members