I have a redirect rule setup like this

#redirect old phpbb links to xenforo links
if ( $arg_p ) {
   rewrite ^\/forum\/viewtopic\.php /forum/posts/$arg_p/? permanent;

Which it will send a 301 to any url that matches that regex to the new format.

However I would like to know if there's a way to do this redirect ONLY if the destination URL is valid (in this context valid means it should not return a 404 or 403)

  • Why do you want this and what should happen if it's possible?
    – Alexey Ten
    Jul 2, 2014 at 11:50
  • I'd like to return a clean 404 if destination URL doesn't exist, without redirecting first.
    – Bogdan
    Jul 2, 2014 at 12:01

1 Answer 1


You can't really do this easily, and it's unlikely that you really want to.

If you could do this, it would require a query to your web application for every single incoming request, in addition to serving the request itself, thus approximately doubling the load on your server. This could probably be done with some clever embedded Lua code, but I think you probably don't want to overload your server.

Better to just redirect everything and then let the destination (your new forum) serve a 404 if necessary. This doesn't really have any SEO impact, because the original links were 404s already. But it provides a better user experience, since incoming traffic to dead links will at least see the new forum rather than a plain white nginx 404 page.

  • Well, to be honest SEO IS the reason I wanted to do this. Looking in Webmaster tools I got tons of 404s which I believe are caused by outdated google index. Normally I would expect that after a number of tries google would stop trying to index those links. But it's been almost a week and the index hasn't been updated and I keep getting those 404s errors. I thought this is happening because of the redirects.
    – Bogdan
    Jul 2, 2014 at 13:30
  • Google will keep trying 404s essentially forever, in case the page ever comes back. Jul 2, 2014 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .