Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

linux - NGINX Redirect URL with Query Strings

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&section=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=(.*)&section=(.*)&type=(.*)$ /$1/$2/$3 permanent;
   rewrite ^/(.*)$ /index.php?tag=$1&page=1 last;
}

but it didnt work..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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=(.*)&section=(.*)&type=(.*)$) {
    return 301 /$1/$2/$3;
}
location / {
    ...
}

See this caution on the use of if.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...