Apache allow access by IP range OR HTTP referrer

Hi experts,

I have a requirement to block all web traffic to a site EXCEPT from a certain IP range, plus from my static IP address. I also need to use an uptime monitoring service. The service I am using does not have static IPs and the only distinguishing factor is the referer url.

How can I create rules to satisfy these requirements on Apache (eg in .htaccess)
LVL 10
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mstrelanAuthor Commented:
I figured it out. See below

1. Ignore all where http referer is from example.com
2. Ignore all from ip range
3. Ignore all from ip
4. Forbid everyone else

  RewriteCond %{HTTP_REFERER} !^http://example\.com/.* [NC]
  RewriteCond %{REMOTE_ADDR} !^192\.168\.0\.[1-255]$
  RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1$
  RewriteRule ^(.*)$ /$1 [L,R=403]

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Brad HoweDevOps ManagerCommented:

What you are looking for is the HTTP_REFERER and REMOTE_ADDR Conditional statements. Here is an example .htaccess file you could use in the directory you want to restrict.

RewriteEngine on
RewriteBase /

#If  incoming URL is http://www.yourreferralurl.com. This is just and example
  RewriteCond %{HTTP_REFERER} !^http://www\.yourreferralurl\.com/ [NC]

#If IP is 192.***.***.*** This is just and example
  RewriteCond %{REMOTE_ADDR} ^192\.([0-9][0-1][4-9])\.  
  RewriteRule ^.*$ - [F]
Brad HoweDevOps ManagerCommented:
Correct :), I was just writing up a response for you.
Have a good one.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.