I want to give references like / catalog / xxx -> /catalog/xxx.html

I write a location:

location /catalog/ { rewrite ^/catalog/(.*)$ /catalog/$1.html permanent; } 

But in the end, when referring to the site.ru/catalog/ section, I get to site.ru/catalog/.html

  • location ~ ^/catalog/(.+)$ { return http://site.ru/catalog/$1.html; } location ~ ^/catalog/(.+)$ { return http://site.ru/catalog/$1.html; } better probably so. The permanent is the return 301 URL; - Deadooshka
  • Also does not fit, cyclical redirect is obtained. - T0PT
  • well yes, obviously. location ~ ^/catalog/(.+?)(?!\.html)$ { return http://site.ru/catalog/$1.html; } location ~ ^/catalog/(.+?)(?!\.html)$ { return http://site.ru/catalog/$1.html; } . if there are no dots before .html, then location ~ ^/catalog/([^\.]+)$ easier location ~ ^/catalog/([^\.]+)$ - Deadooshka
  • At the moment, all links of the site.ru/catalog/bmw type are redirected to site.ru/catalog/bmw.html and this is correct, but if there is a slash at the end of the url site.ru/catalog/bmw/, then the rule does not work. - T0PT
  • ^/catalog/(.+?)/?(?!\.html)$ or ^/catalog/([^\.]+?)/?$ ^/catalog/(.+?)/?(?!\.html)$ - Deadooshka

2 answers 2

What do you want? Section site.ru/catalog/ under the location falls? Falls. Under the regular season gets? Also falls. Here is the redirect and it works.

Perhaps you would be more ^/catalog/(.+)$ regular ^/catalog/(.+)$

    Checked on regular expressions, everything is correct $ 1 is an empty string.

    I suppose you need a rule: ^/catalog/([-A-Za-z0-9_'%]+)$ , which does not work on / catalog /, but works on everything else starting with / catalog /, for example, on / catalog / xxx