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

How to forward URL query in nginx dynamically and strip extra params

· 52CE 我爱测

I have a couple thousand URL's that look like the following: https://www.example.com/supplier-shop/u-23452345/s-p/ https://www.example.com/supplier-shop/u-1714128138 https://www.example.com/supplier-shop/u-436877957/s-p https://w…

问题原文

原帖链接 · NooBskie · CC BY-SA 4.0

I have a couple thousand URL's that look like the following:

https://www.example.com/supplier-shop/u-23452345/s-p/
https://www.example.com/supplier-shop/u-1714128138
https://www.example.com/supplier-shop/u-436877957/s-p
https://www.example.com/supplier-shop/u-32452345
https://www.example.com/supplier-shop/u-2345245664
https://www.example.com/supplier-shop/u-23452345/

This is from my legacy website but our new URL structure looks like the following:

www.example.com/seller/xxxxxxxx

I know how to rewrite single URL's but how would I do a catch all case for all of my id's?

已采纳解答

原帖链接 · Brennen Smith · CC BY-SA 4.0

Here's your answer:

location ~ ^/supplier-shop/(?<id>(.*)) {
    rewrite ^ http://$server_name/seller/$id permanent;
}

Works as well for n level directories as well. Example:

> GET /supplier-shop/asdasd/asdasd HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.47.0
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.10.3 (Ubuntu)
< Date: Thu, 28 Jun 2018 00:10:02 GMT
< Content-Type: text/html
< Content-Length: 194
< Connection: keep-alive
< Location: http://www.example.com/seller/asdasd/asdasd

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

资料来源与适用范围

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

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

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