NGINX用正则表达式重写

时间:2013-08-10 22:28:17

标签: regex nginx rewrite

try_files $uri/index.html $uri @moved;
  location @moved {
  rewrite ^/categories/dogs-c-food/products/xyz-product$ /categories/dogs/food/products/xyz-product permanent;
  rewrite ^/categories/dogs-c-leash/products/xyz-product$ /categories/dogs/leash/products/xyz-product permanent;
  rewrite ^/categories/cats-c-litter-box/products/xyz-product$ /categories/cats/litter-box/products/xyz-product permanent;
}

想要使用一行正则表达式并将-c-替换为/

1 个答案:

答案 0 :(得分:0)

请尝试使用此

try_files $uri/index.html $uri @moved;

location @moved {
    rewrite ^/categories/(.+)-c-(.+)$ "/categories/$1/$2" permanent;
}

请注意斜杠,因为$ URI始终以斜杠(/)开头,因此在重定向到新的“移动”方案后,它应该是相同的。