Answer the question
In order to leave comments, you need to log in
How can this Nginx rule be optimized?
Hi, friend.
Faced such a problem. You need to set up a 301 redirect to all pages in the index after changing the CNC structure.
Pages look like:
https://example.org/cho-takoe-habr-blog/ where blog is the category and chto-takoe-habr is the title of the article. After the redirect, the format should be https://example.org/blog/chto-takoe-habr.html.
On the one hand, everything is simple, but I don’t know how to understand that this is exactly a link to a former article and not to a page.
I did this, according to my logic, it should work, but it doesn’t work (more precisely, it redirects all pages, regardless of the conditions):
set $allow301 "";
if ($request_uri ~ ^.*\.html) {
set $allow301 "3";
}
if ($request_uri ~ ^\/(.*?)(-|)(?!.*\-)(.*?)(\/|$)) {
set $catslug "$1$3";
set $allow301 "no";
}
if ($catslug != ""){
set $allow301 "$allow3011";
}
if ($catslug ~ ^[^\/]+$){
set $allow301 "2$allow301";
}
if ($allow301 = "21"){
rewrite ^\/(.*?)(-|)(?!.*\-)(.*?)(\/|$) /$3/$1.html permanent;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question