1

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?

MrWhite
  • 11,643
  • 4
  • 25
  • 40
Yuriy Nemtsov
  • 145
  • 1
  • 5

1 Answers1

2

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.

larsks
  • 41,276
  • 13
  • 117
  • 170