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.)
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With