1

We are trying to rewrite to another folder if the file does not exist in the document root, but does exist in the other folder.

The other folder is in a completely different location, which is located using "Alias" in the vhosts.

So, what we have so far (from this post How to rewrite URI from root if file exists in folder?) is:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/legacy/
RewriteRule ^(.*)$ legacy/$1 [QSA,L]

This works to an extent, but seems to direct everything to the legacy folder, not just when the file doesn't exist in the first location and does exist in legacy.

Thanks in advance for any help, Jack.

Jack
  • 11
  • 1
  • 2

1 Answers1

0

In your draft, the first RewriteCond checks to see whether or not the webpage exists in the root, but there is no check whether or not the webpage exists in the alternate folder, hence too much gets redirected. (The second RewriteCond is apparently either some sort of anti-loop check [which doesn't work because this is an internal-redirect not an external-redirect] or some sort of direct access to the alternate directory [which depending on your usage may not even be a good idea]).

Such a simple thing to state seems surprisingly complicated to implement. The best I could find (hopefully somebody else will know better) is:

# parse the current local filename into parts we can manipulate
# (weird construct and regex in first RewriteCond are because 
#  mod_rewrite never does any variable substitution on the righthand side)
RewriteCond %{DOCUMENT_ROOT},%{REQUEST_FILENAME} ^([^,]*),\1/*(.*)/+(.*)$
RewriteRule ^ - [E=CURPATH:/%2,E=CURNAME:%3]

# allow direct access to the alternate folder
#  (omit this entire ruleset if that's not desired)
RewriteCond %{ENV:CURPATH} !^/*legacy(?:/|$)
RewriteRule ^ - [L]

# do the checks and make the change if necessary
RewriteCond %{DOCUMENT_ROOT}/%{ENV:CURPATH}/%{ENV:CURNAME} !-f
RewriteCond %{DOCUMENT_ROOT}/legacy/%{ENV:CURPATH}/%{ENV:CURNAME} -f
RewriteRule ^ /legacy/%{ENV:CURPATH}/%{ENV:CURNAME} [L]

(Note this is slightly more than just a solution to your stated problem. It also tries to be more general: working in all subdirectories, working even when slashes are doubled, and working on different Apaches with subtly different configurations.)