G
G
Gadget Freeman2014-09-27 06:07:45
Nginx
Gadget Freeman, 2014-09-27 06:07:45

How to rewrite rewrites under nginx?

Good day.
I ask for help in rewriting rewrites under nginx.
nginx version: nginx/1.4.1
The original piece of rules from .htaccess looks like this:

RewriteRule ^images/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$ media.php?size=$1&id=$2&image=square
RewriteRule ^images/norm/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$ media.php?size=$1&id=$2&image=normal
RewriteRule ^images/w/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$ media.php?size=$1&id=$2&image=square&t=w
RewriteRule ^images/h/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$ media.php?size=$1&id=$2&image=square&t=h
RewriteRule ^images/h/norm/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$ media.php?size=$1&id=$2&image=normal&t=h

RewriteCond %{REQUEST_URI} /images/w/norm/([0-9]{2,4})/([0-9,a-z,A-Z,-_]{3})([0-9,a-z,A-Z,-_]+)\.jpg
RewriteCond /var/www/html/_cache/norm_cache_s%1w/%2/%2%3.jpg -f
RewriteRule ^images/w/norm/([0-9]{2,4})/([0-9,a-z,A-Z,-_]{3})([0-9,a-z,A-Z,-_]+)\.jpg http://host.info/_cache/norm_cache_s$1w/$2/$2$3.jpg [L]

RewriteRule ^images/w/norm/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$ media.php?size=$1&id=$2&image=normal&t=w

Your version (did not take off):
location ~ "/images/w/norm/([0-9]{2,4})/([0-9,a-z,A-Z,-_]{3})([0-9,a-z,A-Z,-_]+)\.jpg"  {
rewrite "/images/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$" /media.php?size=$1&id=$2&image=square;
rewrite "/images/norm/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$" /media.php?size=$1&id=$2&image=normal;
rewrite "/images/w/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$" /media.php?size=$1&id=$2&image=square&t=w;
rewrite "/images/h/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$" /media.php?size=$1&id=$2&image=square&t=h;
rewrite "/images/h/norm/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+).jpg$" /media.php?size=$1&id=$2&image=normal&t=h;

         if (!-f /var/www/_cache/norm_cache_s$1w/$2$2.$3.jpg) {
rewrite "/images/w/norm/([0-9]{2,4})/([0-9,a-z,A-Z,-_]{3})([0-9,a-z,A-Z,-_]+)\.jpg" http://st.host.info/_cache/norm_cache_s$1w/$2/$2$3.jpg break;
                        }
                        try_files "/_cache/norm_cache_s$1/$2/$2$3.jpg" /index-$1_$2_$2_$3_$4.html;
                }

I tried various converters ( convertor ), but they did not help much in this situation.
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Philipp, 2014-09-27
@Carbonat

I think the quotes are redundant. Try separating all the rules into one location and debug the first rule first, then the second, and so on.

location ~ /images/w/norm/([0-9]{2,4})/([0-9,a-z,A-Z,-_]{3})([0-9,a-z,A-Z,-_]+)\.jpg  {
  rewrite ^/images/([0-9]{2,4})/([0-9,a-z,A-Z,-_]+)\.jpg$ /media.php?size=$1&id=$2&image=square;
}

Sysoev has everything well written, because nginx.org/ru/docs/http/ngx_http_rewrite_module.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question