nginx仅在原始ID为404时才重写URL

时间:2014-08-08 07:06:56

标签: nginx rewrite

我有这个重写规则:

rewrite ^/components/com_jshopping/files/img_products/full_(.*)$ http://www.domain.tdl/components/com_jshopping/files/img_products/$1 permanent;

它将以full_开头的每个图像请求重定向到没有前缀的同一图像。

现在我只想在请求的图像不存在的情况下应用此规则(404错误)。

我该怎么办?

1 个答案:

答案 0 :(得分:2)

您可以将try_files用于将进行重定向的命名位置:

location /components/com_jshopping/files/img_products/full_ {
    try_files $uri @redirect;
}

location @redirect {
    rewrite ^(/components/com_jshopping/files/img_products/)full_(.*)$ http://www.domain.tdl$1$2 permanent;
}