NGINX:404时,请尝试使用带前缀的相同URL [英] NGINX: when 404, try the same URL with prefix

查看:392
本文介绍了NGINX:404时,请尝试使用带前缀的相同URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的NGINX配置文件中创建一个规则以添加此功能:

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

如果:

www.mydomain.com/*给出404,我希望NGINX重定向到www.mydomain.com/*-obsolete.

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/testpage404要返回,我希望NGINX重定向到www.mydomain.com/testpage-obsolete尝试避免404.

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.

那是怎么做的?

推荐答案

例如:

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

更复杂的情况,显示过时的html文件:

More complex case, showing obsolete html files:

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

这篇关于NGINX:404时,请尝试使用带前缀的相同URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆