问题
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?
回答1:
Looks like I figured out. The issue is solved in two steps:
All static files must be placed into
wsgi/staticif 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.htaccessfile wich is not allows to configure such stuff. So I can't make apache serve assets fromwsgi/imgor$OPENSHIFT_DATA_DIR/img. I have to create symlinks to those dirs insidewsgi/staticlike:/wsgi /static /img -> (symlink) $OPENSHIFT_DATA_DIR/imgNow we can access to images via
site.com/static/img/1.jpg.Now we need map
site.com/static/img/1.jpgtosite.com/img/1.jpginwsgi/.htaccess. Something like:RewriteRule ^img/(.+)$ /static/img/$1 [L] or RewriteRule ^/img/(.+)$ /static/img/$1 [L]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.jpgin 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^:RewriteRule /img/(.+)$ /static/img/$1 [L]Now we can use
site.com/img/1.imgbut alsosite.com/randomstuff/img/1.imgwill work. So I useRewriteCondto filter such URLs.
Here is my final solution:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/img/
RewriteRule /img/(.+)$ /static/img/$1 [L]
来源:https://stackoverflow.com/questions/25912675/openshift-python-2-7-and-static-files-with-htaccess