Jeffrey Green
Jeffrey Green

Reputation: 173

.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 path/to/subdirectory, 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?

Edit: Here's the .htaccess 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 file, I want to match the entire request prior to the word file and redirect there. This is so that if a request is to

    example.com/a/b/c/file[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 a/b/c/$1.file since I won't know for absolute certainty the a/b/c part.

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

    example.com/a/b/c/fileacs.file

And want to redirect to:

    example.com/a/b/c/file.file

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:

    example.com/file.file

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.

Upvotes: 1

Views: 275

Answers (2)

Jeffrey Green
Jeffrey Green

Reputation: 173

The solution is to use the a RewriteCond on the %{REQUEST_URI} (thanks anubhava!) that checks matches the entire request except for the %{REQUEST_FILENAME} without capturing it, using a lookahead. Write out this with the %1 followed by the desired filename. See the following:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} (.*)-file(?!\.file) [NC]
    RewriteRule ^ %1.file [L,R=301]

The %1 now holds the path up to the directory that the .htaccess is stored, plus any prefix to the filename. This doesn't match the remainder of the request, but rather looksahead to ensure you're not actually requesting the file you would like to redirect to (causing a loop).

Upvotes: 0

anubhava
anubhava

Reputation: 784908

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]

Upvotes: 2

Related Questions