Trying to get all requests to mydomain.com/downloads/* to run through download.php. Because this could also be mydomain.com/some/path/downloads/* I need dynamic RewriteBase which I took from https://stackoverflow.com/a/21063276 however when using the created Environment variable in the RewriteCond pattern it doesn't work and the logs show the raw %{ENV:BASE} instead of expanding the variable.
.htaccess in domain.com/some/path:
RewriteEngine On
RewriteBase /
# Dynamic RewriteBase from https://stackoverflow.com/a/21063276
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_URI} ^%{ENV:BASE}/?download/(.*) [NC]
RewriteRule .* %{ENV:BASE}/download.php?file=%1&%{QUERY_STRING} [L]
Rewrite Log:
(3) [perdir /var/www/123/some/path/] strip per-dir prefix: /var/www/123/some/path/download/file/225 -> download/file/225
(3) [perdir /var/www/123/some/path/] applying pattern '^(.*)$' to uri 'download/file/225'
(4) [perdir /var/www/123/some/path/] RewriteCond: input='/some/path/download/file/225::download/file/225' pattern='^(.*?/)(.*)::\\2$' => matched
(5) setting env variable 'BASE' to '/some/path/'
(3) [perdir /var/www/123/some/path/] add path info postfix: /var/www/123/some/path/download -> /var/www/123/some/path/download/file/225
(3) [perdir /var/www/123/some/path/] strip per-dir prefix: /var/www/123/some/path/download/file/225 -> download/file/225
(3) [perdir /var/www/123/some/path/] applying pattern '^(.*)$' to uri 'download/file/225'
(4) [perdir /var/www/123/some/path/] RewriteCond: input='/var/www/123/some/path/download' pattern='!-f' => matched
(4) [perdir /var/www/123/some/path/] RewriteCond: input='/some/path/download/file/225' pattern='^%{ENV:BASE}/?download/' [NC] => not-matched
(1) [perdir /var/www/123/some/path/] pass through /var/www/123/some/path/download
BASE is set correctly in line 4, however it's not expanded when the RewriteCond is checked in Line 9. If it were being expanded it should work just fine from what I can tell.
Does anyone know what's wrong and how to get this to work correctly?
Server version: Apache/2.2.15 (Unix)