Jeffrey Green Jeffrey Green - 1 year ago 83
Apache Configuration Question

.htaccess and rewrite from subdirectories inside unknown parent directory

So I have an htaccess file in a subdirectory and whenever I try to rewrite the url, it redirects to the document_root and not the subdirectory where the htaccess resides. Now, under normal circumstances, I'd rewrite it with the path to the subdirectory with

, but I won't know what the exact path will be. Is there a way either, through an Apache environment variable or something else, to write out that path?

Here's the
file so far.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule (.*-file) a/b/c/$1.file
RewriteRule (.*-file) $1.file

So, I'm trying to, if the request contains the word
, I want to match the entire request prior to the word file and redirect there. This is so that if a request is to[any characters here].file

the request will be redirected to the right file. To reiterate, the problem is that I am trying to redirect within the subdirectory. So when I say Rewrite $1, I want that to include the entire request and not just what matched in the REQUEST_FILENAME. And the reason I need it to do that is because I can't simply put
since I won't know for absolute certainty the

Edit 2: Examples:
So, an example is that I'd send a request like:

And want to redirect to:

Where I do not know a/b/c/. I have an actual regex and set of rules for the real-world use of this redirect, so don't mind the ridiculous nature of this example.

But currently it's redirecting to:

Which does not exist and even if it did, I do not want to redirect there. I've read about Rewrite Context, but can't find out anything substantial about it nor if it's the cause for this. Thank you, in advance.

Answer Source

You can use this rule to capture any path before fileacs.file and use that as bach-reference in RewriteRule:

RewriteEngine On

RewriteCond ^(.*)/file[^.]+\.file$ [NC]
RewriteRule ^ %1/file.file [L,R=302]