Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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/ [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.

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.*$ http://www.mysite.org/otherdir/ [R=302,L]

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.

share|improve this question

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/.*$ http://mysite.org/myotherdir

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

share|improve this answer
Hi dflock - that doesn't seem to work. I'm still redirected to the other dir even though I'm accessing it from one of the IPs in my directive. I've updated my question with what I exactly have in my htaccess. – Mark Bubel May 14 '13 at 17:40
Hmmm. That should work. I've updated the answer a bit - could you try it again? – Duncan Lock May 14 '13 at 17:57
I also added RewriteLogLevel 9 in, which will add re-write debugging information to the Apache log file for that site - what does it say? – Duncan Lock May 15 '13 at 16:37
WHen I add this, my site throws a 500 error, and the message is: RewriteLog not allowed here – Mark Bubel May 15 '13 at 20:02
yea, I am using my public Ip that is static. Thanks for the help; I'll ask around other places, and if I come across a solution to my problem, I'll post it here. – Mark Bubel May 15 '13 at 23:02
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.