问题原文
原帖链接 · Bijan Zand · CC BY-SA 4.0
I want to check if a parameter is present in a url in nginx and then rewrite.How can i do that?
For e.g if url is http://website.com/Cat-8/ID-1/Ads-4-3?page=1 then redirect user to http://website.com/Cat-8/ID-1/Ads-4-3
Only redirect page with Cat- and end with ?page=1
E.G:
http://website.com/Cat-8/ID-1/Ads-4-3?page=1
redirect to
http://website.com/Cat-8/ID-1/Ads-4-3
Another E.G:
http://website.com/node/Item21?page=1
for example i dont want this pages redirect
http://website.com/node/Item21?page=1
With my code all url has been redirect !
there is :
if ($arg_page != "") { return 301 $uri; }
已采纳解答
原帖链接 · Richard Smith · CC BY-SA 4.0
You could use rewrite instead of return, as it can:
- target just those URLs that begin with
Cat-, and - it can easily remove the original query string
For example:
if ($arg_page != "") { rewrite ^(/Cat-.*)$ $1? permanent; }
Important: The if block behaves differently when placed inside a location block. So this code snippet is designed to be placed inside the server block.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
