【发布时间】:2021-01-11 05:54:29
【问题描述】:
我想将使用 URL 的查询参数类型的旧链接重写为新的 URL 样式。
例如
-
example.com/page?id=1->example.com/page/1 -
example.com/otherpage?id=1->example.com/otherpage/1
目前我使用evil if进行以下配置。
if ($args ~* "id=(.*)") {
set $w1 $1;
rewrite .* $scheme://$host/page/$w1? permanent;
}
注意:我使用的是 CloudFront,并且依赖于上面的主机头。
如果上述内容在服务器块中,没有其他位置块 - 这是否符合在 NGINX 配置中对if 的非恶意使用?此外,以上仅支持/page/。使该部分适用于otherpage 和其他页面的任何更好的想法?
我看到了一些其他想法讨论使用地图,但我不太确定如何将它们组合在一起?我的想法是这样的:
map $args_id ?? {
default ?
??
}
...
server {
...
???
}
更新: 根据@Ivan 的回答,这是我的最终解决方案:
server {
listen 80;
root /usr/share/nginx/html;
index index.html index.htm;
# Handle legacy requests
if ($args ~* "id=(.*)") {
set $w1 $1;
rewrite ^ $scheme://$host$uri/$w1? permanent;
}
}
【问题讨论】:
标签: nginx url-rewriting nginx-config