OpenShift, python 2.7 and static files with htaccess

I am trying to set up apache for static files for urls like site.com/img/bla.jpg

. Python cartridge + flask.

I know there is a pre-configured alias for the directory wsg/static

, so we can use site.com/static/bla.jpg

. But I need an additional static directory.

Project structure:

/wsgi
  .htaccess
  /img -> $OPENSHIFT_DATA_DIR/some/path (soft link)
  /static
  application

      

In the backend I have linked mysite.com/img/<filename>

for testing, if apache or backend processes files - it returns the string "ok [filename]".

I've tried the following configurations 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 it, the regex doesn't match the request url and apache is just passing information to the 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 i1_3d265689.png

RewriteEngine On
RewriteBase /
RewriteRule /img/1.jpg https://ssl.gstatic.com/gb/images/i1_3d265689.png [L]

      

Note: there is no RewriteCond.

So how do you get this to work?

Again I want apache to serve mysite.com/img/<filename>

as wsg/img/<filename>

.

Is there something wrong with openshift or am I missing something?

+3


source to share


1 answer


Looks like I get it. The problem was solved in two steps:

  • All static files must be placed in wsgi/static

    if you want apache to serve them for you (not your script backend). You cannot configure apache to use a different directory for this, as you can only manipulate with a file .htaccess

    that does not allow for such stuff to be configured. So I cannot get apache to serve assets from wsgi/img

    or $OPENSHIFT_DATA_DIR/img

    . I have to create symlinks for these dirs inside wsgi/static

    , like:

    /wsgi
        /static
            /img -> (symlink) $OPENSHIFT_DATA_DIR/img
    
          

    We can now access the images via site.com/static/img/1.jpg

    .

  • Now we want to display site.com/static/img/1.jpg

    in the site.com/img/1.jpg

    in wsgi/.htaccess

    . Something like:

    RewriteRule ^img/(.+)$ /static/img/$1 [L]
    or
    RewriteRule ^/img/(.+)$ /static/img/$1 [L]
    
          

    And it won't work because regexp forces the search from the beginning of the URL path ( ^

    ). The problem is that the OpenAhift apache url is <wsgi app name>/%{REQUEST_URI}

    (at least for RewriteRule

    ). application/img/1.jpg

    in my situation. Therefore ^img/(.+)$

    , neither ^/img/(.+)$

    will nor will match the url. I'm not an apache expert, but maybe this template configuration link can help someone sort out the url path issue. So the solution is to remove ^

    :

    RewriteRule /img/(.+)$ /static/img/$1 [L]
    
          

    Now we can use site.com/img/1.img

    , but it will also work site.com/randomstuff/img/1.img

    . So I use RewriteCond

    to filter such urls.



Here is my final solution:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/img/
RewriteRule /img/(.+)$ /static/img/$1 [L]

      

+3


source







All Articles