homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Variable in htaccess
RewriteRule question

Msg#: 4167032 posted 8:37 am on Jul 9, 2010 (gmt 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]



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

Msg#: 4167032 posted 5:42 pm on Jul 10, 2010 (gmt 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").


Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved