K
K
Kostushko2015-04-08 08:35:24
Nginx
Kostushko, 2015-04-08 08:35:24

Nginx how to make redirect for specific uri returning 404?

For cms (joomla) running on nginx+php-fpm, you need to make a redirect for pagination pages returning 404.
For example, there are 4 pagination pages in the auto category:
/auto.html
/auto/page-2.html
/auto/page- 3.html
/auto/page-4.html
Other, non-existing pages of this category (/auto/page-5.html ... /auto/page-999.html) should return 301 redirects to the first page of the category (/ auto.html).
There are a lot of categories, the site is filling up, so when the site engine from the /auto/page-5.html page starts to issue not 404, but 200 redirects in nginx should stop working.
The code that seems to work redirects (to /auto.html) both existing pages (/auto/page-2.html ... /auto/page-4.html) and non-existing ones (/auto/page- 5.html.../auto/page-999.html).

location ~* /(auto)/page-\d+\.html {
    error_page 404 = @page;
  }
  
  location @page {
    rewrite /(.*?)/page-\d+\.html http://site.ru/$1.html permanent;
  }

  error_page 404 =404       /404.html;
  location ~* \.php$          {
    include                   fastcgi_def;
  }
  location /                  {try_files $uri /index.php?$args;}

Answer the question

In order to leave comments, you need to log in

3 answer(s)
K
Kostushko, 2015-04-08
@Kostushko

Worked as it should in this configuration:

error_page 404 =404         /404.html;
  location ~* \.php$          {
    include                   fastcgi_def;
    include                   add/cachephp;
    if ($request_uri ~ /(.+)/page-\d+\.html) {
      error_page 404 = @page;
    }
    fastcgi_intercept_errors on;
  }
  location /                  {try_files $uri /index.php?$args;}
  
  location @page {
    if ($request_uri ~ /(.+)/page-\d+\.html) {return 301 /$1.html;}
    return 410;
  }

Thanks Alexei Ten for the tip. Previous Configuration
location @page {
    rewrite /(.+)/page-\d+\.html /$1.html redirect;
    return 404;
}

Didn't work, because @page gets an already converted request that was sent to index.php, in order to form a redirect, you must use the original request.

L
Lynn "Coffee Man", 2015-04-08
@Lynn

You can try like this:

error_page 404 /404.html;

location / {
    try_files $uri /index.php;
}

location ~* \.php$ {
    include fastcgi_def;
    fastcgi_intercept_errors on;
    error_page 404 = @page;
}

location @page {
    rewrite /(.+)/page-\d+\.html /$1.html redirect;
    return 404;
}

P
polozad, 2015-04-08
@polozad

location /auto {
try_files $uri /auto.html; 
root /bla/bla/auto
}

try_files - checks for the presence of files in the specified path, if there are none, uses the last argument in the directive with an internal redirect.
nginx.org/ru/docs/http/ngx_http_core_module.html#t...
You can build a fence with a named location like @fallback, but this is redundant, I think.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question