[英]Nginx: Rewrite to remove last path from $uri
I'm moving all document from /docs/path/to/sub/*.pdf
to their parent folder, /docs/path/to/*.pdf
. 我将所有文档从
/docs/path/to/sub/*.pdf
移到其父文件夹/docs/path/to/*.pdf
。
How to redirect from old url: 如何从旧网址重定向:
example.com/docs/path/to/sub/file.pdf
to new url 到新网址
example.com/docs/path/to/file.pdf
I'm using method below but not work: 我正在使用下面的方法,但不起作用:
location /docs/ {
rewrite ^/docs/([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)/([a-zA-Z_]+)\.(.*)$ /docs/$1/$2/$3/$5 permanent;
}
You're using permanent
flag which means a redirect, the URL would probably return 404 after the redirect, try replacing permanent
with last
and see if it works. 您使用的是
permanent
标志,这表示重定向,URL在重定向后可能会返回404,请尝试用last
替换permanent
,看看是否可行。
EDIT: 编辑:
I think your mistake is that you are matching the extension, so you are doing the counting wrong, you don't have enough capturing blocks. 我认为您的错误是您正在匹配扩展名,因此您在计算错误时没有足够的捕获块。 I'd like to do it in a more dynamic way though, not sure if this will work.
不过,我想以一种更动态的方式进行操作,不确定是否可行。
location ^~ /docs(.*)/([^/]+)/(.*) {
try_files $uri @redirect;
}
location @redirect {
return 301 $scheme$http_host/docs$1$3;
}
if not you can change your current rewrite, assuming you have 5 capturing blocks, 如果没有,您可以更改当前重写,假设您有5个捕获块,
/docs/$1/$2/$4$5
last 2 are file+extension 最后2个是文件+扩展名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.