# Redirect some IP addresses

**URL:** <https://www.ruby-forum.com/t/redirect-some-ip-addresses/208868>\
**Category:** NGINX\
**Created:** [June 29, 2011, 7:12am UTC](https://www.ruby-forum.com/t/redirect-some-ip-addresses/208868 "2011-06-29T07:12:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [June 29, 2011, 7:12am UTC](https://www.ruby-forum.com/t/redirect-some-ip-addresses/208868/1 "2011-06-29T07:12:25Z")

</div>

Hi forumers,

As I can’t test it, I’d like to know if my redirect code is correct or  
not:

location / {  
if ($remote\_addr ~ “^(62.160.71.0/24)”){  
rewrite ^/(._)$ /operation\_deny3m.html  
permanent;  
}  
if ($remote\_addr ~ “^(84.233.174.48/28)”){  
rewrite ^/(._)$ /operation\_deny3m.html  
permanent;  
}  
if ($remote\_addr ~ “^(80.118.39.160/27)”){  
rewrite ^/(.\*)$ /operation\_deny3m.html  
permanent;  
}  
try\_files $uri $uri/ @dokuwiki;  
}

```
    location @dokuwiki {
        # Rewriting clean URLs
        rewrite ^/_media/(.*)

```

/lib/exe/fetch.php?media=$1 last;  
…

Thanks,  
Jiff

Posted at Nginx Forum:

> **[redirect some IP addresses](https://forum.nginx.org/read.php?2,210944,210944#msg-210944)**
>
> Hi forumers, As I can't test it, I'd like to know if my redirect code is correct or not: location / { if ($remote\_addr ~ "^(62.160.71.0/24)"){ rewrite ^/(.\*)$ /operation\_deny3m.html permanent;

---

<div class="post-metadata">

**Author:** ![Jiff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@Jiff](https://www.ruby-forum.com/u/Jiff)\
**Post date:** [June 29, 2011, 7:29am UTC](https://www.ruby-forum.com/t/redirect-some-ip-addresses/208868/2 "2011-06-29T07:29:42Z")

</div>

Hello!

On Wed, Jun 29, 2011 at 01:11:38AM -0400, Jiff wrote:

> Hi forumers,
> 
> As I can’t test it, I’d like to know if my redirect code is correct or  
> not:
> 
> location / {  
> if ($remote\_addr ~ “^(62.160.71.0/24)”){

No, this is clearly incorrect and won’t work. Use

```
deny 62.160.71.0/24;

```

instead, see [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpAccessModule) for details.  
Normally this will return 403 error, though this may be easily  
redefined with error\_page directive (see  
[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpCoreModule#error_page)).

If you do really need “if” testing instead of client addresses,  
use geo module ([GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/HttpGeoModule)) for CIDR  
testing.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Jiff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/dc4da7/32.png) [@Jiff](https://www.ruby-forum.com/u/Jiff)\
**Post date:** [July 7, 2011, 5:54pm UTC](https://www.ruby-forum.com/t/redirect-some-ip-addresses/208868/3 "2011-07-07T17:54:17Z")

</div>

Thanks, deny + redirecting toward my own 403 page will do 🙂

Posted at Nginx Forum:

> **[Re: redirect some IP addresses](https://forum.nginx.org/read.php?2,210944,212006#msg-212006)**
>
> Hi forumers, As I can't test it, I'd like to know if my redirect code is correct or not: location / { if ($remote\_addr ~ "^(62.160.71.0/24)"){ rewrite ^/(.\*)$ /operation\_deny3m.html permanent;
