P
P
Pesetsu2019-03-16 01:39:57
Nginx
Pesetsu, 2019-03-16 01:39:57

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;
    }

What is wrong here, and is it possible to somehow optimize the rule? I'm a noob in regular seasons, I understand the most elementary ones.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Softer, 2019-03-16
@Pesetsu

First - if is avil
Second - (somehow, act by analogy)
rewrite ^/(.+)-([a-z])$ /$2/$1.html permanent;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question