You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Andrew Schulman <an...@alumni.utexas.net> on 2010/12/21 16:40:55 UTC

[users@httpd] Re: RewriteCond how to set variable

> I have the following RewriteCond.  I put numbers in front of each line
> for reference:
> 
> 1)      RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
> 
> 2)      RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
> 
> 3)      RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
> 
> 4)      RewriteRule ^/calendar
> http://test.webservices.illinois.edu/calendar/list/%1?skinId=%2
> <http://test.webservices.illinois.edu/calendar/list/%251?skinId=%252>
> [L]
> 
> I just learned that when you use %1 and %2 in line 4 it will only
> reference the last RewriteCond.  So in line 4 the %1 comes from line 3
> but the %2 is blank.  I actually wanted the values from line 2 & 3 to be
> inserted into line 4.
> 
> How do you set a local variable in line 2 and 3 so that in line 4 I can
> reference them?  

This isn't too pretty.

RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
RewriteRule .* - [E=calId:%1]

RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
RewriteRule .* - [E=skinId:%1]

RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
RewriteCond %{calId} .
RewriteCond %{skinId} .
RewriteRule ^/calendar
http://test.webservices.illinois.edu/calendar/list/%{calId}?skinId=%{skinId}
[L]

(I'm not sure if your original RewriteRule came through correctly - I think
I've interpreted it right.)

I'm not 100% sure about the use of %{calId} and %{skinId} in the
substitution string of the RewriteRule.  I think it's right, but try it.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Re: RewriteCond how to set variable

Posted by Björn Zettergren <bj...@basefarm.se>.
On 12/21/2010 07:12 PM, Andrew Schulman wrote:
>> My email client displayed what you wanted me to insert incorrectly.  I
>> put the code in correctly but it still does not work.  I added R=301 to
>> the end so that it would redirect so I could check the apache logs.
>
> Good debugging trick.

For debugging, also use:

RewriteLog /tmp/rewrite.log
RewriteLogLevel 9

/Björn

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Re: RewriteCond how to set variable

Posted by "Campbell, Lance" <la...@illinois.edu>.
I did find a work around by doing the follow:

RewriteCond %{QUERY_STRING} !XSL=NONE [NC] 
RewriteCond %{QUERY_STRING} skinId=([0-9]+)(.*)calId=([0-9]) [NC] 
RewriteRule ^/calendar
http://test.webservices.illinois.edu/calendar/list/%3?skinId=%1
[L,R=301] 

RewriteCond %{QUERY_STRING} !XSL=NONE [NC] 
RewriteCond %{QUERY_STRING} calId=([0-9]+)(.*)skinId=([0-9]) [NC] 
RewriteRule ^/calendar
http://test.webservices.illinois.edu/calendar/list/%1?skinId=%3
[L,R=301]

Thanks,

Lance Campbell
Software Architect/DBA/Project Manager
Web Services at Public Affairs
217-333-0382


-----Original Message-----
From: Andrew Schulman [mailto:andrex@alumni.utexas.net] 
Sent: Tuesday, December 21, 2010 12:13 PM
To: users@httpd.apache.org
Subject: [users@httpd] Re: RewriteCond how to set variable

> My email client displayed what you wanted me to insert incorrectly.  I
> put the code in correctly but it still does not work.  I added R=301
to
> the end so that it would redirect so I could check the apache logs.

Good debugging trick.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server
Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re: RewriteCond how to set variable

Posted by Andrew Schulman <an...@alumni.utexas.net>.
> My email client displayed what you wanted me to insert incorrectly.  I
> put the code in correctly but it still does not work.  I added R=301 to
> the end so that it would redirect so I could check the apache logs.

Good debugging trick.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re: RewriteCond how to set variable

Posted by Andrew Schulman <an...@alumni.utexas.net>.
> My email client displayed what you wanted me to insert incorrectly.  I
> put the code in correctly but it still does not work.  I added R=301 to
> the end so that it would redirect so I could check the apache logs.
> 
> The apache log shows this:
> uob017.admin.uiuc.edu - - [21/Dec/2010:11:46:23 -0600] "GET
> /calendar/Calendar?calId=7&skinId=2155 HTTP/1.1" 301 417 "-"
> "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13)
> Gecko/20101203 Firefox/3.6.13"
> uob017.admin.uiuc.edu - - [21/Dec/2010:11:46:23 -0600] "GET
> /calendar/list/ HTTP/1.1" 302 - "-" "Mozilla/5.0 (Windows; U; Windows NT
> 6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13"
> 
> You can see that the values did not get put into the URL.

OK.  This is where I think the documentation is a little unclear.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule says that
the substitution string can include "server-variables as in rule condition
test-strings (%{VARNAME})".  The question is whether %{calId} and %{skinId}
qualify as "server variables", or just plain old "environment variables".
It may be the latter, in which case my suggestion won't work.  Sorry.

Eric suggested several other approaches which all look feasible.

Good luck,
Andrew.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Re: RewriteCond how to set variable

Posted by "Campbell, Lance" <la...@illinois.edu>.
My email client displayed what you wanted me to insert incorrectly.  I
put the code in correctly but it still does not work.  I added R=301 to
the end so that it would redirect so I could check the apache logs.

The apache log shows this:
uob017.admin.uiuc.edu - - [21/Dec/2010:11:46:23 -0600] "GET
/calendar/Calendar?calId=7&skinId=2155 HTTP/1.1" 301 417 "-"
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.13)
Gecko/20101203 Firefox/3.6.13"
uob017.admin.uiuc.edu - - [21/Dec/2010:11:46:23 -0600] "GET
/calendar/list/ HTTP/1.1" 302 - "-" "Mozilla/5.0 (Windows; U; Windows NT
6.1; en-US; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13"

You can see that the values did not get put into the URL.

This is what I have added:

# Store value of the calId field, if present
RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
RewriteRule .* - [E=calId:%1]

# Store value of the skinId field, if present
RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
RewriteRule .* - [E=skinId:%1]

# If both the calId and skinId fields are present, and XSL=NONE is
not...
RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
RewriteCond %{calId} .
RewriteCond %{skinId} .
RewriteRule ^/calendar
http://test.webservices.illinois.edu/calendar/list/%{calId}?skinId=%{ski
nId} [L,R=301]


Thanks,

Lance Campbell
Software Architect/DBA/Project Manager
Web Services at Public Affairs
217-333-0382


-----Original Message-----
From: Andrew Schulman [mailto:andrex@alumni.utexas.net] 
Sent: Tuesday, December 21, 2010 11:05 AM
To: users@httpd.apache.org
Subject: [users@httpd] Re: RewriteCond how to set variable

> Thanks for your response.  I get the following error message:
> RewriteCond: unknown flag 'E'

That's because the E flag is only valid on a RewriteRule statement, not
RewriteCond.  The mod_rewrite docs show this.

So, in order to store a match result in an environment variable, you
have
to use a degenerate RewriteRule statement, that matches everything and
rewrites nothing, but sets an environment variable:

RewriteRule .* - [E=calID:%1]

.* = match everything
-  = rewrite nothing
E  = set an environment variable

(I warned you it wasn't pretty.)  The RewriteRule documentation explains
these things.

Here's an annotated version of what I wrote before:

# Store value of the calId field, if present
RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
RewriteRule .* - [E=calId:%1]

# Store value of the skinId field, if present
RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
RewriteRule .* - [E=skinId:%1]

# If both the calId and skinId fields are present, and XSL=NONE is
not...
RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
RewriteCond %{calId} .
RewriteCond %{skinId} .
# ... then rewrite:
RewriteRule ^/calendar
http://test.webservices.illinois.edu/calendar/list/%{calId}?skinId=%{ski
nId}
[L]

Try this and see if it works for you.
Andrew.

 
> What I used:
> RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
> RewriteCond %{QUERY_STRING} calId=([0-9]+) [E=CALID:%1]
> RewriteCond %{QUERY_STRING} skinId=([0-9]+) [E=SKINID:%1]
> RewriteRule ^/calendar/list
>
http://test.webservices.illinois.edu/calendar/list/%{CALID}?skinId=%{SKI
> NID} [L]
> 
> 
> 
> Thanks,
> 
> Lance Campbell
> Software Architect/DBA/Project Manager
> Web Services at Public Affairs
> 217-333-0382
> 
> 
> -----Original Message-----
> From: Andrew Schulman [mailto:andrex@alumni.utexas.net] 
> Sent: Tuesday, December 21, 2010 9:41 AM
> To: users@httpd.apache.org
> Subject: [users@httpd] Re: RewriteCond how to set variable
> 
> > I have the following RewriteCond.  I put numbers in front of each
line
> > for reference:
> > 
> > 1)      RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
> > 
> > 2)      RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
> > 
> > 3)      RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
> > 
> > 4)      RewriteRule ^/calendar
> > http://test.webservices.illinois.edu/calendar/list/%1?skinId=%2
> >
<http://test.webservices.illinois.edu/calendar/list/%251?skinId=%252>
> > [L]
> > 
> > I just learned that when you use %1 and %2 in line 4 it will only
> > reference the last RewriteCond.  So in line 4 the %1 comes from line
3
> > but the %2 is blank.  I actually wanted the values from line 2 & 3
to
> be
> > inserted into line 4.
> > 
> > How do you set a local variable in line 2 and 3 so that in line 4 I
> can
> > reference them?  
> 
> This isn't too pretty.
> 
> RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
> RewriteRule .* - [E=calId:%1]
> 
> RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
> RewriteRule .* - [E=skinId:%1]
> 
> RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
> RewriteCond %{calId} .
> RewriteCond %{skinId} .
> RewriteRule ^/calendar
>
http://test.webservices.illinois.edu/calendar/list/%{calId}?skinId=%{ski
> nId}
> [L]
> 
> (I'm not sure if your original RewriteRule came through correctly - I
> think
> I've interpreted it right.)
> 
> I'm not 100% sure about the use of %{calId} and %{skinId} in the
> substitution string of the RewriteRule.  I think it's right, but try
it.
> 
> 
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server
> Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
> 
> 
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server
Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
> 


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server
Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


[users@httpd] Re: RewriteCond how to set variable

Posted by Andrew Schulman <an...@alumni.utexas.net>.
> Thanks for your response.  I get the following error message:
> RewriteCond: unknown flag 'E'

That's because the E flag is only valid on a RewriteRule statement, not
RewriteCond.  The mod_rewrite docs show this.

So, in order to store a match result in an environment variable, you have
to use a degenerate RewriteRule statement, that matches everything and
rewrites nothing, but sets an environment variable:

RewriteRule .* - [E=calID:%1]

.* = match everything
-  = rewrite nothing
E  = set an environment variable

(I warned you it wasn't pretty.)  The RewriteRule documentation explains
these things.

Here's an annotated version of what I wrote before:

# Store value of the calId field, if present
RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
RewriteRule .* - [E=calId:%1]

# Store value of the skinId field, if present
RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
RewriteRule .* - [E=skinId:%1]

# If both the calId and skinId fields are present, and XSL=NONE is not...
RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
RewriteCond %{calId} .
RewriteCond %{skinId} .
# ... then rewrite:
RewriteRule ^/calendar
http://test.webservices.illinois.edu/calendar/list/%{calId}?skinId=%{skinId}
[L]

Try this and see if it works for you.
Andrew.

 
> What I used:
> RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
> RewriteCond %{QUERY_STRING} calId=([0-9]+) [E=CALID:%1]
> RewriteCond %{QUERY_STRING} skinId=([0-9]+) [E=SKINID:%1]
> RewriteRule ^/calendar/list
> http://test.webservices.illinois.edu/calendar/list/%{CALID}?skinId=%{SKI
> NID} [L]
> 
> 
> 
> Thanks,
> 
> Lance Campbell
> Software Architect/DBA/Project Manager
> Web Services at Public Affairs
> 217-333-0382
> 
> 
> -----Original Message-----
> From: Andrew Schulman [mailto:andrex@alumni.utexas.net] 
> Sent: Tuesday, December 21, 2010 9:41 AM
> To: users@httpd.apache.org
> Subject: [users@httpd] Re: RewriteCond how to set variable
> 
> > I have the following RewriteCond.  I put numbers in front of each line
> > for reference:
> > 
> > 1)      RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
> > 
> > 2)      RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
> > 
> > 3)      RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
> > 
> > 4)      RewriteRule ^/calendar
> > http://test.webservices.illinois.edu/calendar/list/%1?skinId=%2
> > <http://test.webservices.illinois.edu/calendar/list/%251?skinId=%252>
> > [L]
> > 
> > I just learned that when you use %1 and %2 in line 4 it will only
> > reference the last RewriteCond.  So in line 4 the %1 comes from line 3
> > but the %2 is blank.  I actually wanted the values from line 2 & 3 to
> be
> > inserted into line 4.
> > 
> > How do you set a local variable in line 2 and 3 so that in line 4 I
> can
> > reference them?  
> 
> This isn't too pretty.
> 
> RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
> RewriteRule .* - [E=calId:%1]
> 
> RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
> RewriteRule .* - [E=skinId:%1]
> 
> RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
> RewriteCond %{calId} .
> RewriteCond %{skinId} .
> RewriteRule ^/calendar
> http://test.webservices.illinois.edu/calendar/list/%{calId}?skinId=%{ski
> nId}
> [L]
> 
> (I'm not sure if your original RewriteRule came through correctly - I
> think
> I've interpreted it right.)
> 
> I'm not 100% sure about the use of %{calId} and %{skinId} in the
> substitution string of the RewriteRule.  I think it's right, but try it.
> 
> 
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server
> Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
> 
> 
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>    "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
> 


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


RE: [users@httpd] Re: RewriteCond how to set variable

Posted by "Campbell, Lance" <la...@illinois.edu>.
Thanks for your response.  I get the following error message:
RewriteCond: unknown flag 'E'

What I used:
RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
RewriteCond %{QUERY_STRING} calId=([0-9]+) [E=CALID:%1]
RewriteCond %{QUERY_STRING} skinId=([0-9]+) [E=SKINID:%1]
RewriteRule ^/calendar/list
http://test.webservices.illinois.edu/calendar/list/%{CALID}?skinId=%{SKI
NID} [L]



Thanks,

Lance Campbell
Software Architect/DBA/Project Manager
Web Services at Public Affairs
217-333-0382


-----Original Message-----
From: Andrew Schulman [mailto:andrex@alumni.utexas.net] 
Sent: Tuesday, December 21, 2010 9:41 AM
To: users@httpd.apache.org
Subject: [users@httpd] Re: RewriteCond how to set variable

> I have the following RewriteCond.  I put numbers in front of each line
> for reference:
> 
> 1)      RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
> 
> 2)      RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
> 
> 3)      RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
> 
> 4)      RewriteRule ^/calendar
> http://test.webservices.illinois.edu/calendar/list/%1?skinId=%2
> <http://test.webservices.illinois.edu/calendar/list/%251?skinId=%252>
> [L]
> 
> I just learned that when you use %1 and %2 in line 4 it will only
> reference the last RewriteCond.  So in line 4 the %1 comes from line 3
> but the %2 is blank.  I actually wanted the values from line 2 & 3 to
be
> inserted into line 4.
> 
> How do you set a local variable in line 2 and 3 so that in line 4 I
can
> reference them?  

This isn't too pretty.

RewriteCond %{QUERY_STRING} calId=([0-9]+) [NC]
RewriteRule .* - [E=calId:%1]

RewriteCond %{QUERY_STRING} skinId=([0-9]+) [NC]
RewriteRule .* - [E=skinId:%1]

RewriteCond %{QUERY_STRING} !XSL=NONE [NC]
RewriteCond %{calId} .
RewriteCond %{skinId} .
RewriteRule ^/calendar
http://test.webservices.illinois.edu/calendar/list/%{calId}?skinId=%{ski
nId}
[L]

(I'm not sure if your original RewriteRule came through correctly - I
think
I've interpreted it right.)

I'm not 100% sure about the use of %{calId} and %{skinId} in the
substitution string of the RewriteRule.  I think it's right, but try it.


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server
Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org