[olug] apache mod_rewrite question

Adam Haeder adamh at omaha.org
Wed Feb 27 23:15:37 UTC 2002


Of course, as soon as I post a question to the list, I figure it out. This 
solution seems a little kludgy, but it works.

RewriteCond %{REQUEST_URI} ^/0/0/.*/1.htm$
RewriteRule ^/0/0/(.*)/1.htm$       -       [E=STATUS:$1]

RewriteCond %{REQUEST_URI} ^/0/0/.*/1.htm$
RewriteRule ^/0/0/.*/1.htm$ /0/0/%{ENV:STATUS}/2.htm [L]


The key here was finding a way to reference the "matched" directory in the 
RewriteCond line, since I could never be sure of the full path to the 
directory (assuming potential infinite subdirs). The answer here lies in 
the use of the "E=" directive and the $1 backreference. "E=" allows you to 
set an environment variable that can then be referenced by a cgi script or 
(in this case) in another RewriteRule line. The $1 is a backreference 
which gives you access to a part of your RewriteRule first argument that 
is contained in parenthesis. In this case, my first argument was

^/0/0/(.*)/1.htm$

The parenthesis part is

(.*)

which means I can reference whatever that resolved to as $1. If I had a 
second parenthesis part, I could reference that as $2, etc, up to $9.

So I set the made-up environment variable STATUS to the value of $1, and I 
can then use the %{ENV:STATUS} string to get the value of that environment 
variable and use it to construct my rewrite string, which I do in the 2nd 
RewriteRule line.

If you ever do stuff with mod_rewrite, keep these pages handy:
The Official Apache documentation on mod_rewrite:
http://httpd.apache.org/docs/mod/mod_rewrite.html

Ralf S. Engelschall's (the author of mod_rewrite) guide to mod_rewrite, 
with examples:
http://www.engelschall.com/pw/apache/rewriteguide/

-- 
Adam Haeder
Technical Coordinator, AIM Institute
adamh at omaha.org
(402) 345-5025 x115



-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_

For help contact olug-help at bstc.net - run by ezmlm
to unsubscribe, send mail to olug-unsubscribe at bstc.net
or `mail olug-unsubscribe at bstc.net < /dev/null`
(c)2001 OLUG http://www.olug.org

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_




More information about the OLUG mailing list