大网数据广告活动
服务器运维问答

How Check Url Parametr and Redirect with Nginx

· 52CE 我爱测

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 redir…

问题原文

原帖链接 · 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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2023-10-29)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。