JasonGenX
JasonGenX

Reputation: 5434

NGINX: when 404, try the same URL with prefix

I would like to create a rule in my NGINX config file to add this functionality:

if:

www.mydomain.com/* gives out 404 I would like NGINX to redirect to www.mydomain.com/*-obsolete.

for example, if a user navigates to:

www.mydomain.com/testpage and 404 is to be returned, I'd like NGINX to redirect to www.mydomain.com/testpage-obsolete to try and avoid the 404.

How is that done?

Upvotes: 0

Views: 377

Answers (1)

Ivan Shatsky
Ivan Shatsky

Reputation: 15478

For example:

    location / {
        try_files $uri $uri-obsolete $uri/ $uri-obsolete/ =404;
    }

More complex case, showing obsolete html files:

    location ~ ^(.*)/([^/]+)\.(html?)$ {
        set $file $1/$2;
        set $ext $3;
        try_files $file.$ext $file-obsolete.$ext =404;
    }

Upvotes: 1

Related Questions