Mobile app version of vmapp.org
Login or Join
Gloria169

: Conditional RedirectMatch based on IP How do I use this RedirectMatch with limiting by IP address? This is what I have right now, and it works as is: RewriteRule ^dir.*$ http://www.mysite.org/otherdir/

@Gloria169

Posted in: #Htaccess #Redirects

How do I use this RedirectMatch with limiting by IP address?

This is what I have right now, and it works as is:

RewriteRule ^dir.*$ www.mysite.org/otherdir/ [R=302,L]


I want this to work only if not accessed from two different IP addresses. A visit from any other IP address should execute this code in my .htaccess.

Edit:
Exact code as it appears in my .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^127.0.0.1 [OR]
RewriteCond %{REMOTE_ADDR} !^xx.xxx.xxx.x #masked on purpose
RewriteRule ^dir.*$ www.mysite.org/otherdir/ [R=302,L]
</IfModule>


Edit 2:
I am now using this in my .htaccess file
RewriteCond %{REMOTE_ADDR} !^127.0.0.1
RewriteRule ^conference - [F]

This works on my local machine, but if I switch to my external IP address, it's completely ignored.

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Gloria169

2 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

Ok, I finally got this to work (sort of). Here's my final code

RewriteCond %{REMOTE_ADDR} !=123.456.789.0
RewriteRule ^secretdir- [F]


I removed the escaping of the periods, and now it seems to work with my external IP address. I'm not sure if that's what is actually causing the problem, but now it works.

I say that it sort of works because (1) it's not redirecting to the dir I want it to, and (2) it's returning a 403 status. I'm almost ready to take /secretdir live, so I wish I could return a 302 status.

I hope this helps anyone who runs in to a bizarre problem like this.

10% popularity Vote Up Vote Down


 

@Cugini213

You need to add conditions to your match, like this:

RewriteCond %{REMOTE_ADDR} !=123.123.123.123 [OR]
RewriteCond %{REMOTE_ADDR} !=123.123.123.124
RedirectMatch 302 ^/mydir/.*$ mysite.org/myotherdir

Obviously, replace the 123 stuff with the actual IP addresses in question, remembering to escape the dots, as shown.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme