问题原文
原帖链接 · Nick Rolando · CC BY-SA 3.0
I have the following in my nginx.conf:
location ~* /collections.*?products/([^/]+)/?$ {
rewrite ^/collections.*?products/([^/]+)/?$ /$1.html;
rewrite ^([^_]*)_([^_]*)_(.*)$ $1-$2-$3;
rewrite ^([^_]*)_(.*)$ $1-$2 permanent;
}
To rewrite requests such as
"/collections/products/someproduct/" to "/someproduct.html"
"/collections/products/some_product/" to "/some-product.html"
"/collections/products/some_other_product/" to "/some-other-product.html"
However I can only get a 301 redirect to occur if the last rewrite directive (containing the permanent flag) matches and processes, e.g. my 2nd example. In the other 2 instances I get a 302 temporary redirect. How can I process these multiple rewrite directives in this location block and return 301 redirect regardless which ones match? If I put a permanent flag on all rewrite directives, it will stop processing after first match.
已采纳解答
原帖链接 · Richard Smith · CC BY-SA 3.0
You can convert _ to - recursively and independently of the rewrite...permanent.
For example:
location ~* /collections.*?products/([^/]+)/?$ {
rewrite ^(.*)_(.*)$ $1-$2 last;
rewrite ^/collections.*?products/([^/]+)/?$ /$1.html permanent;
}
The second rewrite is executed only after the first rewrite fails to find any more underscores. See this document for more.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
