【问题标题】:Nginx redirect if 404 and request contain certin words如果 404 和请求包含某些单词,则 Nginx 重定向
【发布时间】:2020-07-19 07:33:08
【问题描述】:

如果页面不存在我需要重定向(404)

重定向必须是智能的 如果 URL 包含单词“test”,则将 301 重定向到 new_page,否则抛出 404

# define error page
error_page 404 = @notfound;

# error page location redirect 302
location @notfound {
    
   if ($request_uri = "test"){
        return 301 /new_page ;
    }

}

【问题讨论】:

标签: nginx nginx-config


【解决方案1】:

试试这个:

map $request_uri $error404 {
    ~test    error404_redirect;
    default  error404_notfound;
}

server {
    ...
    error_page 404 = @$error404;
    location @error404_notfound {
        return 404;
    }
    location @error404_redirect {
        return 301 /new_page;
    }
}

【讨论】:

    猜你喜欢
    • 2018-02-21
    • 1970-01-01
    • 2016-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-09
    • 2015-06-12
    相关资源
    最近更新 更多