# Return 404 from map module default value

**URL:** <https://www.ruby-forum.com/t/return-404-from-map-module-default-value/210354>\
**Category:** NGINX\
**Created:** [August 5, 2011, 3:21pm UTC](https://www.ruby-forum.com/t/return-404-from-map-module-default-value/210354 "2011-08-05T15:21:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 5, 2011, 3:21pm UTC](https://www.ruby-forum.com/t/return-404-from-map-module-default-value/210354/1 "2011-08-05T15:21:26Z")

</div>

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:

> **[return 404 from map module default value](https://forum.nginx.org/read.php?2,213431,213431#msg-213431)**
>
> 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 ~\*...

---

<div class="post-metadata">

**Author:** ![zflairz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/dc4da7/32.png) [@zflairz](https://www.ruby-forum.com/u/zflairz)\
**Post date:** [August 5, 2011, 3:29pm UTC](https://www.ruby-forum.com/t/return-404-from-map-module-default-value/210354/2 "2011-08-05T15:29:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zflairz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/dc4da7/32.png) [@zflairz](https://www.ruby-forum.com/u/zflairz)\
**Post date:** [August 5, 2011, 3:33pm UTC](https://www.ruby-forum.com/t/return-404-from-map-module-default-value/210354/3 "2011-08-05T15:33:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zflairz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/dc4da7/32.png) [@zflairz](https://www.ruby-forum.com/u/zflairz)\
**Post date:** [August 5, 2011, 4:46pm UTC](https://www.ruby-forum.com/t/return-404-from-map-module-default-value/210354/4 "2011-08-05T16:46:53Z")

</div>

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

```
    location / {
            index index.html;
    }

```

so [www.example.com](http://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:

> **[Re: return 404 from map module default value](https://forum.nginx.org/read.php?2,213431,213435#msg-213435)**
>
> 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 ~\*...

---

<div class="post-metadata">

**Author:** ![zflairz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/dc4da7/32.png) [@zflairz](https://www.ruby-forum.com/u/zflairz)\
**Post date:** [August 6, 2011, 3:54am UTC](https://www.ruby-forum.com/t/return-404-from-map-module-default-value/210354/5 "2011-08-06T03:54:22Z")

</div>

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](http://wiki.nginx.org/HttpRewriteModule#return))  
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:

> **[Re: return 404 from map module default value](https://forum.nginx.org/read.php?2,213431,213440#msg-213440)**
>
> 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 ~\*...

---

<div class="post-metadata">

**Author:** ![zflairz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/dc4da7/32.png) [@zflairz](https://www.ruby-forum.com/u/zflairz)\
**Post date:** [August 6, 2011, 4:31am UTC](https://www.ruby-forum.com/t/return-404-from-map-module-default-value/210354/6 "2011-08-06T04:31:18Z")

</div>

On 6 Ago 2011 02h53 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 ([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpRewriteModule#return))  
> 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](http://my.redirected.url.here);

— appa

---

<div class="post-metadata">

**Author:** ![zflairz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/dc4da7/32.png) [@zflairz](https://www.ruby-forum.com/u/zflairz)\
**Post date:** [August 5, 2011, 5:44pm UTC](https://www.ruby-forum.com/t/return-404-from-map-module-default-value/210354/7 "2011-08-05T17:44:23Z")

</div>

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](http://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.
