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?

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .