Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite: If the file exists in another directory, serve that one instead

I have a website at example.com/test/. Lets say the website is laid out as such:

example.com
└── test/
    ├── assets/
    │   └─ stylesheet.css
    │
    ├── .htaccess
    └── index.php

index.php here is the router, as is apparently cool to do nowadays.

Whenever the user requests a page like example.com/test/stylesheet.css, I want to check to see if assets/ has that file, and if so, serve that file instead of giving the URL to index.php. Ideally, the following would work:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond assets/%{REQUEST_FILENAME} -f
RewriteRule ^(.+)$ assets/$1

But since %{REQUEST_FILENAME} is an absolute path, assets/%{REQUEST_FILENAME} turns out to be something like assets/home/public/test/stylesheet.css. %{REQUEST_URI} is no better: it turns into assets/test/stylesheet.css. I also looked at this question, but the answer didn't work either.

Is there any way, without resorting to PHP, to do this? (If not, I'll just use PHP's readfile, but I don't want to worry about LFI or anything.)

like image 789
Waleed Khan Avatar asked Aug 20 '12 01:08

Waleed Khan


1 Answers

Try using the %{DOCUMENT_ROOT} and %{REQUEST_URI} vars

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/assets/%{REQUEST_URI} -f
RewriteRule ^(.+)$ assets/$1

EDIT: I see, try this instead:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/([^/]+)/(.+)$
RewriteCond %{DOCUMENT_ROOT}/%1/assets/%2 -f
RewriteRule ^(.*)$ /%1/assets/%2 [L,R]
like image 132
Jon Lin Avatar answered Oct 01 '22 02:10

Jon Lin