Рewrite how not to rewrite?

Hello!

There is a problem with mod_rewrite from nginx.

Read the forums, Sysoeva and Google, finding the cause can not

The problem:


There's the rewrite entry

rewrite ^/blog/([^/]+)/?(.*)?$ /blog/category/$2?$1 break;


there is a directory on the server

blog
category
name


by my logic queries such as

/blog/test/ sent to /blog/category

/blog/test/name/ sent on /blog/category/name/

and in practice, other than a request

/blog/test/name

at the end of the slash is not, then, in this case nginx for no reason, no this does redirect to

blog/category/name/?category=test


why?
October 8th 19 at 01:20
5 answers
October 8th 19 at 01:22
Solution
Figured out problem
If you set rewrite_log on
you can see why
is looping and you can see the logic of nginx-a
last cycle I block redirect
October 8th 19 at 01:24
Example config
server {
 listen 8080;
 server_name site.lo www.site.lo;

 access_log W:/hosting/site.lo/nlogs/access.log main;

 error_page 500 502 503 504 /50x.html;
 location = /50x.html {
 root /usr/local/www/nginx-dist;
}

 location ^~ /res/ {}

 location = / {}

 location ~ /\.ht {
 deny all;
}

 rewrite ^/blog/([^/]+)/?(.*)?$ /blog/category/$2?$1 break;

 location / {}

 root W:/hosting/site.lo/www/;
 index index.php index.html index.htm;

 #For debug only
 autoindex on;
}
October 8th 19 at 01:26
^/blog/([^/]+)/?(.*)?$
That's why before '$' is '?'?

/blog/category/$2?$1
need what for '?$1'?

at the end of the slash is not, then, in this case nginx for no reason, no this does redirect to
blog/category/name/?category=test

generally have to do an internal rewrite to /blog/category/name?test, but apparently at this location there is some script that redirects thinking that this is a test category
address with a slash at the end dobrosovestnosty in /blog/category/?test and /blog/category/name/?test where the url of the rolls.
$ to indicate the end of the row
no script, not even html pages no - Haylee.Bahringer commented on October 8th 19 at 01:29
October 8th 19 at 01:28
Removed $ — the problem remained

^/blog/([^/]+)/?(.*)? /blog/category/$2?$1 break;
the logic of the regexp, so that requests
/blog/test/
/blog/unix/
caught in /blog/category/
/blog/test/name — directory name lies in category
/blog/unix/name
caught in /blog/category/name

(.*) — it should be that everything else went in the settings to do this ?$1
Again
no .htaccess and or scripts in the directories there - Haylee.Bahringer commented on October 8th 19 at 01:31
This problem is on Windows and Unix build of nginx
Version nginx — 1.0.14 - Haylee.Bahringer commented on October 8th 19 at 01:34
October 8th 19 at 01:30
Another interesting behavior of mod_rewrite found

on the query
site.lo/blog/category
rewrite ^/blog/([^/]+)/(.*)$ /blog/category/ break;
it gives the page (that you want), and puts header on redirect
site.lo/blog/category/

Find more questions by tags ApacheNginx