在 nginx 重写中从 uri 中删除连字符和单词



实际链接结构如下:

domain.com/abc/xyz/lmn/1.0.0-alpha/abc.html 我们想要的是重定向到

domain.com/abc/xyz/lmn/1.0/abc.html

通过在nginx中使用重写

如何在nginx中做到这一点?

谢谢

像这样的东西?我不确定其他要求是什么,但如果它只是一个静态 url,它应该可以工作。

location = domain.com/abc/xyz/lmn/1.0.0-alpha/abc.html {
rewrite ^(.*)$ domain.com/abc/xyz/lmn/1.0/abc.html;
}

如果你有很多这样的,看看map指令。

但无论如何,我的建议是生成一个简单的页面,告诉用户他们正在寻找的版本已经过时,并且有一个新的版本可用,而不是进行自动重定向:

location /abc/zyz/lmn/1.0 {
rewrite ^(/abc/zyz/lmn/1.0)(?:.0-alpha)(abc.html)$ $1$2 break;
return 404
"<!DOCTYPE html><title>404 Not Found: $request_uri</title>
<h1>404 Not Found: $request_uri</h1>
<h2>The version you're seeking is outdated.
Try <a href='$uri'>$uri</a> instead.</h2>"
}

最新更新