[olug] apache mod_rewrite question

Adam Haeder adamh at omaha.org
Wed Feb 27 22:38:18 UTC 2002


OK, I'm pretty familiar with mod_rewrite. I use it in quite a few domains, 
doing image and page swapping, and serving certain files if the http host 
is this, etc.

A normal setup looks like this (this is under a VirtualHost directive):

RewriteEngine on
RewriteRule ^/group/corner0w\.htm$ /group/corner2w.htm

This says any request for /group/corner0w.htm, give them 
/group/corner2w.htm instead.

Now on to the real problem.
I have a virtual host with a directory structure like 0/0/[0-9], meaning 
valid directories are 0/0/0, 0/0/1, 0/0/2, etc. Actually, this could 
potentially go on indefinitely (so 0/0/0/0/0/0/2 would be valid). I need 
to replace all instances of the file 1.htm in any subdirectory with the 
file 2.htm in that same directory. So, for example, 0/0/0/0/0/0/1.htm 
would become 0/0/0/0/0/0/2.htm. I'm trying to come up with a simple 
mod_write ruleset that will accomplish this. What I have so far:
(note - I know that 0/0/ will always be at the top of the dir tree)

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/0/0/.*/1.htm$
RewriteRule 1\.htm$ /2.htm [L]

This, however, doesn't work. The RewriteCond matches, but then the 
replacement looks like this:  0/0/0/0/1.htm -> 2.htm, which fails, because 
/2.htm doesn't exist, I needed 0/0/0/0/2.htm.

Is there any way, in the second argument to the RewriteRule directive, to 
use the directory string that was actually matched in the RewriteCond 
statement? In all my other instances of mod_rewrite, I'm always rewriting 
to a fixed string, where in this case, I want to create the destination 
string based on the directory that actually matched.

I know I can do this by putting a unique .htaccess file in each dir and 
using the RewriteBase command, but I could be talking thousands of 
potential directories.

Any help appreciated! Thanks.

-- 
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