问题
I have this NGINX configuration:
root /var/www/web;
index index.php;
server_name domain.com;
access_log off;
error_log on;
location / {
rewrite ^/(.*)$ /index.php?tag=$1&page=1 last;
}
Now, I want to redirect url something like "domain.com/index.php?tag=1§ion=2&type=3" to "domain.com/tag/section/type"
how can I do that, where should I put the code? please help,
Thankyou
I already tried:
location / {
rewrite ^/index\.php?tag=(.*)§ion=(.*)&type=(.*)$ /$1/$2/$3 permanent;
rewrite ^/(.*)$ /index.php?tag=$1&page=1 last;
}
but it didnt work..
回答1:
The rewrite and location directives use a normalized URI which does not include the query string.
To test the query string, you will need to consult the $request_uri or $args variable using an if statement and/or map directive.
The advantage of using $request_uri is that it contains the original request and will help to avoid a redirection loop.
If you only have one redirection to perform, the map solution is probably overfill.
Try:
if ($request_uri ~ ^/index\.php\?tag=(.*)§ion=(.*)&type=(.*)$) {
return 301 /$1/$2/$3;
}
location / {
...
}
See this caution on the use of if.
来源:https://stackoverflow.com/questions/54329332/nginx-redirect-url-with-query-strings