Nginx rewrite and 2 parameters

2019-09-15 18:19发布

问题:

I have this rules in apache:

RewriteRule ^seccion/([^/\.]+)/?$ index.php?tipo=seccion&slug=$1 [L]
RewriteRule ^seccion/([^/\.]+)/pag/([0-9]+)$ index.php?tipo=seccion&slug=$1&page=$2 [QSA,L]

Both work perfect, however in nginx I have this 2

rewrite ^/seccion/([^\?]+)$ /index.php?tipo=seccion&slug=$1 last;
rewrite ^/seccion/([^\?]+)$/pag/([0-9]+)$ /index.php?tipo=seccion&slug=$1&pag=$2 last;

The second one does not work, only parameter one works but actually gets the 'pay' and the page number added, example

http://example.com/seccion/sports/pag/4

the %slug gets: sportspag4

any clue what I'm doing wrong ?

回答1:

Please try this configuration(your second rule should be first, like in an example):

rewrite ^/seccion/([^/]+)/pag/([0-9]+)$ /index.php?tipo=seccion&slug=$1&pag=$2 last;

rewrite ^/seccion/(.*)$ /index.php?tipo=seccion&slug=$1 last; 


回答2:

There is an extra dollar sign in your second rewrite rule that is matching the end of the line and terminating the match. Also, your first rewrite rule is currently matching everything, it should either come after the second rule or be more restrictive in its match. Try this:

rewrite ^/seccion/([^/]+)/?$ /index.php?tipo=seccion&slug=$1 last;
rewrite ^/seccion/([^/]+)/pag/([0-9]+)$ /index.php?tipo=seccion&slug=$1&pag=$2 last;


标签: nginx