Return 404 from map module default value

I’m using map module to redirect URLs which have been removed (but
indexed by search engine).
But how can I fall back to http 404 response for urls not present in my
redirect list?

I’ve tried following, but I got 200 instead of 404:

    location ~* ^/404$ {
            return 404;
    }

    error_page 404 = /404.html;

    map $uri $redirected_uri {
            default /404; #here?
            include /etc/nginx/redirected_uri.txt;
    }

    location ~* ^/.+ {
            if (!-f $request_filename) {
                    rewrite ^ $redirected_uri permanent;
            }
    }

Posted at Nginx Forum:

Hello!

On Fri, Aug 05, 2011 at 09:21:11AM -0400, zflairz wrote:

            }
    }

You return 301 redirect here for all non-existing files. If you
want to return 404, try something like this instead:

map $uri $redirected {
    default "";
    include /path/to/list;
}

error_page 404 = /404.html;

location / {
    try_files $uri = @redirect;
}

location @redirect {
    if ($redirected) {
        rewrite ^ $redirected premanent;
    }

    return 404;
}

Maxim D.

On Fri, Aug 05, 2011 at 09:21:11AM -0400, zflairz wrote:

            }
    }

First, you do not need regexex here:

  •     location ~* ^/404$ {
    
  •     location  =  /404  {
    
  •     location ~* ^/.+ {
    
  •     location     /   {
    

The configuraiton should look like this:

http {

map $uri  $redirected_uri {
    default  "";
    include  /etc/nginx/redirected_uri.txt;
}

server {
    location / {
        try_files  $uri  @redirect;
    }

    location @redirect {
        if ($redirected_uri) {
            return  301  $redirected_uri;
        }

        return 404;
    }
}

}


Igor S.

I think I need “location ~* ^/.+” instead of “location /” because I have
this:

    location / {
            index index.html;
    }

so www.example.com will read /index.html, while in the case of “location
/”, it can’t find / in mapping, then return 404. Am I right?

Posted at Nginx Forum:

okay, I see. By adding a slash ($uri/) solved the index problem.

Another minor stuff is: I didn’t see following return syntax on the
website (Module ngx_http_rewrite_module)
Is the uri after return code newly added (as an optional value)? I’m
using Nginx 1.0.4. Thanks.

return 301 $redirected_uri;

Posted at Nginx Forum:

On 6 Ago 2011 02h53 WEST, [email protected] wrote:

okay, I see. By adding a slash ($uri/) solved the index problem.

Another minor stuff is: I didn’t see following return syntax on the
website (Module ngx_http_rewrite_module)
Is the uri after return code newly added (as an optional value)? I’m
using Nginx 1.0.4. Thanks.

return 301 $redirected_uri;

AFAIK, it’s undocumented. If you specifie the URL you can get by with
just a parameter, it issues a temporary redirect.

return http://my.redirected.url.here;

— appa

On Fri, Aug 05, 2011 at 10:46:26AM -0400, zflairz wrote:

I think I need “location ~* ^/.+” instead of “location /” because I have
this:

    location / {
            index index.html;
    }

so www.example.com will read /index.html, while in the case of “location
/”, it can’t find / in mapping, then return 404. Am I right?

location / {
    try_files  $uri  $uri/  @redirect;
    index   index.html;
}


Igor S.