Search code examples
nginxurl-rewritingtracstatic-files

Rewrite rule for nginx?


I'm trying to deploy trac with nginx. I almost have everything working exept for the rewrite rule for serving static files. I need to rewrite this url:

http://trac.domain.tldn/chrome/common/feed.png

to this one:

http://trac.domain.tldn/static/htdocs/common/feed.png

I have this code, but it isn't working:

    location ~ /(.*?)/chrome/common/ {
            rewrite /(.*?)/chrome/common/(.*) /$1/static/htdocs/common/$2 break;
            root    /var/www/domain.tldn/static/trac/static/htdocs/common;
    }

Can you help me with this?


Solution

  • You can do it with the following code:

    location /chrome/common {
        rewrite ^/chrome/common/(.*) /static/htdocs/common/$1 permanent;
    }
    

    Or just use an alias for your files path:

    location /chrome/common {
        alias /var/www/domain.tldn/static/trac/static/htdocs/common;
    }