Here's an interesting problem:

I'm trying to separate a few files on a system into a number of packages, and to be able to access them without explicitly specifying which package a file is in.

Here's an example: Say, a file /package1/one.htm is in /package1 and /package2/two.htm is in /package2. With the configuration below, I'll be able to access them directly, eg. http://localhost/one.htm

RewriteCond %{DOCUMENT_ROOT}/package1%{REQUEST_URI} -f
RewriteRule ^(.*) /package1$1 [L]

RewriteCond %{DOCUMENT_ROOT}/package2%{REQUEST_URI} -f
RewriteRule ^(.*) /package2$1 [L] 

RewriteCond %{DOCUMENT_ROOT}/package3%{REQUEST_URI} -f
RewriteRule ^(.*) /package3$1 [L]

The problem is that I'd like to be able to add more packages, without updating this Apache configuration file (and without having to restart Apache). I was thinking of something along the lines of:

RewriteCond %{DOCUMENT_ROOT}/package(.*)%{REQUEST_URI} -f
RewriteRule ^(.*) /package%1$1 [L]

But, unfortunately, the code above does not work, since it is not possible to take a match from the RewriteCond (the (.*)) and then apply it to the RewriteRule. At least that was my understanding of it.

Can you think of a creative way of solving this problem?


Use a programmatic RewriteMap:

RewriteMap rewriter prog:/path/to/script
RewriteRule ^(.*) %{rewriter:$1} [L]

And then in your script, take whatever part of the request path you want, iterate through your directories, and return the appropriate rewrite string.

  • Interesting. Do you believe there's a way to do this without a script? – Yuriy Nemtsov Dec 17 '09 at 20:28
  • I'm not coming up with anything. – larsks Dec 17 '09 at 22:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.