1

I have a static folder which is not in my document root. I would like to redirect all the files that are not present in document root, but are present in the static folder. (So if the same file is in two places, the file that is not in the static will be served first.) How can I do this ? The following doesn't work:

DocumentRoot /srv/app/client/build/
Alias / /srv/app/client/static

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/
RewriteRule ^(.*)$ /$1

<Location />
    Order allow,deny
    Allow from all
</Location>
Dimitri Kopriwa
  • 177
  • 2
  • 11

2 Answers2

1

From the Apache docs: Redirecting and Remapping with mod_rewrite section "Search for pages in more than one directory", something like:

# first try to find it in root/...
# ...and if found stop and be happy:
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} -f
RewriteRule ^(.+) %{DOCUMENT_ROOT}/$1 [L]

# second try to find it in static/...
# ...and if found stop and be happy:
RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI} -f
RewriteRule ^(.+) %{DOCUMENT_ROOT}/static/$1 [L]
Brian
  • 3,476
  • 18
  • 16
  • my structures filders is like this : client/build with all resources builded from stylus/jade/js and client/static with all the static resources. In your purposition, static folder is in the document root, while my document root is build which doesn't contain the static folder. is it possible like this ? : RewriteCond %{DOCUMENT_ROOT}/../static/%{REQUEST_URI} -f RewriteRule ^(.+) %{DOCUMENT_ROOT}/../static/$1 [L] – Dimitri Kopriwa Mar 27 '15 at 16:13
  • Please add the incoming request and where and in what order your trying to check for a resource to your question. Like users request /mainmenu.css, check in / then /static/. – Brian Mar 27 '15 at 18:08
  • Like users request /mainmenu.css, check in / client/build/, else chen in /client/static/ – Dimitri Kopriwa Mar 30 '15 at 11:21
1

If I understand right, you're trying to emulate Nginx's try_files functionality, in which case it looks like maybe this will work for you.

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
RewriteRule ^/(.*)$ balancer://app_cluster%{REQUEST_URI} [P,QSA,L]
GregL
  • 9,370
  • 2
  • 25
  • 36
  • I try to emulate the express.static function of express server, I haven't tried your solution @GregL, what I am trying can be achieved only using mod_proxy_balancer ? – Dimitri Kopriwa Mar 27 '15 at 16:16