简体   繁体   English

Nginx 404到301基于URI重定向

[英]Nginx 404 to 301 redirect based on URI

I am trying to create a multiple 404 redirects in WordPress, and I am currently using this: 我正在尝试在WordPress中创建多个404重定向,而我目前正在使用此方法:

error_page 404 @foobar;

location @foobar {
    rewrite  .* / permanent;
}

I know this is meant for a redirect to the homepage, but this method is not working at all , for it defaults to the regular WordPress 404.php file that is included with the theme. 我知道这是为了重定向到首页,但是此方法根本不起作用 ,因为它默认使用主题中包含的常规WordPress 404.php文件。 That is fine, but I need to get the redirect working via Nginx instead of WP because I need to create different redirects once a page has been declared a 404. 很好,但是我需要通过Nginx而不是WP来进行重定向,因为一旦页面被声明为404,我就需要创建不同的重定向。

Example: Say I have about 10 categories on my WP Site. 示例:假设我的WP网站上大约有10个类别。 I have posts that come and go, and once a post is gone, I would like to redirect that post (because each post in a certain category has a unique identifier) to the base category. 我有来去去的帖子,一旦帖子消失了,我想将该帖子重定向(因为某个类别中的每个帖子都有唯一的标识符)到基本类别。

Example: redirect non existent post 示例:重定向不存在的帖子

/i-love-soup-cause-it-tastes-so-good-campbellssoup ---> /category/campbellssoup

or 要么

/toystory-3-is-a-great-movie-coolmovies ---> /category/coolmovies

Basically I need to create multiple redirects to multiple locations ONLY AFTER the URI has been established as a 404. Any ideas? 基本上,只有在URI被建立为404之后,我才需要创建到多个位置的多个重定向。有什么想法吗?

Edit: 编辑:

Ok, using 好的,使用

fastcgi_intercept_errors on;

I managed to take the virtual errors out of wordpress. 我设法消除了WordPress的虚拟错误。 Now I have the redirect to @foobar working, but I am unable to get the uri's to transfer to the @foobar location block. 现在我可以重定向到@foobar ,但是我无法将uri转移到@foobar位置块。 I am guessing it is something to do with my try_files in my / location block. 我猜想这与我的/ location块中的try_files有关。

location / {
    #try_files $uri $uri/ /index.php?q=$request_uri;
    try_files $uri $uri/ /index.php?$args;
}

First there is a syntax error in your config, it should be error_page 404 = @fallback . 首先,您的配置中存在语法错误,应该为error_page 404 = @fallback This should work fine, but only if wordpress is generating a 404. My guess is it is including something like 404.php instead. 这应该工作正常, 仅当wordpress生成404时才有效。我猜是它包含了类似404.php的内容。 I'm also not sure if you can change the status code within a named location. 我也不确定您是否可以在指定位置更改状态代码。 The documentation explicitly notes that a fallback location can be used, in the case where the URI does not need to be changed and provides changing the URL using a different syntax that is of no use to you. 该文档明确指出,在不需要更改URI的情况下,可以使用后备位置,并且使用其他不需要的语法来更改URL。 I would first figure out if WordPress is handling the 404 by itself. 我首先要弄清楚WordPress是否自行处理404。 If that's the case, then handle the redirect in that file. 如果是这种情况,请在该文件中处理重定向。 If it is not, then simply create your own page that handles the redirect in the scripting language you're comfortable with. 如果不是,那么只需创建您自己的页面即可处理您喜欢的脚本语言的重定向。 If you can do rewrites in named locations for error pages, then the bottom part of @tcurvelo solution should do the trick. 如果您可以在错误页面的命名位置进行重写,则@tcurvelo解决方案的底部应该可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM