I'm trying to configure apache to serve static files for URLs like site.com/img/bla.jpg
.
Python cartridge + flask.
I know what there is an preconfigured alias for wsg/static
directory, so we can use site.com/static/bla.jpg
. But I need additional static directory.
Project structure:
/wsgi
.htaccess
/img -> $OPENSHIFT_DATA_DIR/some/path (soft link)
/static
application
In the backend I binded mysite.com/img/<filename>
for testing if apache or backend handles files - it returns "ok [filename]" string.
I have tried the following configs in htaccess:
1: site.com/img/1.jpg
-> "ok 1.jpg"
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f # with and without this condition
RewriteRule ^img/(.+)$ /static/$1 [L]
# RewriteRule ^img/(.+)$ /img/$1 [L]
# RewriteRule ^/img/(.+)$ /img/$1 [L]
# RewriteRule ^img/(.+)$ https://ssl.gstatic.com/gb/images/i1_3d265689.png [L]
As I understand regex doesn't match request URL and apache just passes stuff to backend?
2: site.com/img/1.jpg
-> "ok 1.jpg"
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /img/1.jpg https://ssl.gstatic.com/gb/images/i1_3d265689.png [L]
3: site.com/img/1.jpg
-> opens google's i1_3d265689.png
RewriteEngine On
RewriteBase /
RewriteRule /img/1.jpg https://ssl.gstatic.com/gb/images/i1_3d265689.png [L]
Note: no RewriteCond.
So how to make it work?
Again I want apache to serve mysite.com/img/<filename>
as wsg/img/<filename>
.
Is something wrong with openshift or I miss something?
Looks like I figured out. The issue is solved in two steps:
All static files must be placed into
wsgi/static
if you want apache to serve them for you (and not your backend script). You can't configure apache to use another directory for that since you can manipulate only with.htaccess
file wich is not allows to configure such stuff. So I can't make apache serve assets fromwsgi/img
or$OPENSHIFT_DATA_DIR/img
. I have to create symlinks to those dirs insidewsgi/static
like:Now we can access to images via
site.com/static/img/1.jpg
.Now we need map
site.com/static/img/1.jpg
tosite.com/img/1.jpg
inwsgi/.htaccess
. Something like:And it will not work becuase regexp forces to search from the beginning of URL-path (
^
). Problem is what OpenShift's apache URL-path is<wsgi app name>/%{REQUEST_URI}
(at least forRewriteRule
).application/img/1.jpg
in my situation. So neither^img/(.+)$
nor^/img/(.+)$
will not match the URL-path. I'm not an apache expert but maybe this link with template config may help someone to figure out URL-path issue. So solution is to remove^
:Now we can use
site.com/img/1.img
but alsosite.com/randomstuff/img/1.img
will work. So I useRewriteCond
to filter such URLs.Here is my final solution: