Using variables inside patterns?

Oh, the strange things mod_rewrite does!

Using variables inside patterns?

Postby mod35 » Thu Jul 03, 2008 7:43 am

Hello, I'm new to this forum, but i noticed there are very advanced people living here :). Perhaps you'll be able to give me an idea.

Is there any known approach for using variable patterns?
The simpliest I can guess is

RewriteRule .* - [E=ENV:VAR1:(optA|optB|optC)]
RewriteRule ^(%{ENV:VAR1})$ /path%{ENV:VAR1}/ [R,L]

unfortunately is not acceptable :(
and neither is the workaround like

RewriteRule .* - [E=ENV:VAR1:(optA|optB|optC)]
RewriteCond %{ENV:VAR1} ^(.*)$
RewriteRule ^(%1)$ /path%{ENV:VAR1}/ [R,L]

both in red are interpreted literally, no substitution :(

Any idea'll be appreciated.
thank you
mod35
 
Posts: 1
Joined: Wed Jul 02, 2008 12:34 pm

Postby richardk » Fri Jul 04, 2008 1:58 pm

Variables in patterns are not allowed. The closest is to use a back reference you created in the pattern (info), for example
Code: Select all
^(a|b|c)-\1$

will match "a-a", "b-b" or "c-c".

Something like this may work
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule .* - [E=ENV:VAR1:optA#optB#optC]

RewriteCond $1#%{ENV:VAR1}# ^([^#]+)#(.*#)?\1(#.*)?#$ [NC]
RewriteRule ^([^/]+)/?$ /do-something [R,L]


If you go to /optB,
Code: Select all
$1#%{ENV:VAR1}#

will become
Code: Select all
optB#optA#optB#optC#


The pattern takes whatever is before the first # and checks if it exists between any of the #s.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Idiosyncrasies

Who is online

Users browsing this forum: Majestic-12 [Bot] and 1 guest

cron