Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm using this simple .htaccess file in a media uploads folder to allow the customer to see what's been uploaded.

Options +Indexes
IndexOptions +FancyIndexing

Is it possible to add a directive to restrict access to only a few IP's? Generally we're not overly worried about the security of these, which is why we're not protecting them with a user/pass setup. However, a tiny bit of security wouldn't hurt :)

Bonus Question... (really giving away my lack of apache config stuff here): Is it possible to have an if..else type setup so that IF the visitor doesn't have a recognised IP, they are re-directed to a specific page.

Cheers! Ben

share|improve this question

1 Answer 1

up vote 3 down vote accepted

If you want to restrict by IP , simply add

<Directory /path/to/the/folder>
            Options +Indexes
            IndexOptions +FancyIndexing
            Order deny,allow
            Deny from all
            Allow from X.X.X.X
</Directory>

If you have pattern in IP address, you could concise the rule also. Like if you want to allow IP begining with 192.

then

Allow from 192 

will allow all the IP beginning with 192.

Bonus question: you don't want to just prevent page loads from the external networks, but want to send them to a different page, mod_rewrite module of apache can come to rescue. Assuming your local net is 192.168.1.0/24:

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

Play around with it.

share|improve this answer
    
Excellent! That's so useful... just a final gap needing filling, What was this for: RewriteCond !^/page_to_redirect Was this only for use if the redirect page was in the same DIR? –  Mere Development Feb 9 '12 at 15:36
    
yes to prevent loop if the user starts from the very page you wanted to redirect to –  kaji Feb 9 '12 at 15:40
    
Ok that makes sense, thanks mate. –  Mere Development Feb 9 '12 at 17:31

Your Answer

 
discard

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.