Answer the question
In order to leave comments, you need to log in
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
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;
}
location @page {
rewrite /(.+)/page-\d+\.html /$1.html redirect;
return 404;
}
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;
}
location /auto {
try_files $uri /auto.html;
root /bla/bla/auto
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question