I am wanting to restrict access to a website to people inside of my network. If someone from outside hits the site they will be redirected to another page.

How do I configure these two different actions based on IP address?


Since you don't want to just prevent page loads from the external networks, but want to send them to a different page, you will need to use mod_rewrite. Assuming your local net is

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^192\.168\.1
RewriteCond !^/foreigners/page
RewriteRule .* /foreigners/page [R,L]

You don't actually need to restrict the page, because the rewrite rules will ensure that the foreigners never see the page.

Note, this is not completely tested, but should get you down the path you want.


Here is what finally worked! Thanks for everyone's help getting me close.

RewriteEngine On
RewriteCond %{REMOTE_ADDR} !^10\.5
RewriteCond %{REQUEST_URI} !^visitor\.php
RewriteRule .* visitor.php

The restriction is done as follows:

<Directory /var/www/your-root-folder/>
        Options Indexes FollowSymLinks Multiviews
        AllowOverride All

        Order Deny,Allow
        Deny from all
        # Allow only from LAN subnet
        Allow from

Then, if you want an automatic redirection for users outside of your local subnet, you'll want to modify the default handling of "403 Forbidden". My example redirects to "index.php".

ErrorDocument 403 /index.php
  • This won't redirect foreigners to a different page, which is what the poster wants. – Rudedog Dec 8 '09 at 16:41
  • 1
    Putting up a custom 403 for foreigners will work just fine, unless you also have other protected pages (say with passwords) that shouldn't display the foreigners' 403. – Rudedog Dec 8 '09 at 16:54
  • Using ErrorDocument to showing a different page might be an ok solution, or it might not. Depending on the content of the redirected page an 403 respons may or may not be suitable. – andol Dec 8 '09 at 17:05
  • 1
    In addition, since you're redirecting to another page inside the document root, the client won't be able to see the 403 page since it's still protected by the IP address restrictions. – Rudedog Dec 8 '09 at 17:07

try this

order deny, allow

deny from all

allow from 192.168

  • This won't redirect foreigners to a different page, which is what the poster wants. – Rudedog Dec 8 '09 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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