Control access to files at directory level

.htaccess is most often used to restrict or deny access to individual files and folders. A typical example would be an "includes" folder. Your site's pages can call these included scripts all they like, but you don't want users accessing these files directly, over the web. In that case you would drop an .htaccess file in the includes folder with content something like this.

# no one gets in here!
deny from all



which would deny ALL direct access to ANY files in that folder. You can be more specific with your conditions, for instance limiting access to a particular IP range, here’s a handy top-level rule for a local test server.

# no nasty hackers in here!
order deny,allow
deny from all
allow from 192.168.0.0/24



# this would do the same.
allow from 192.168.0



Generally these sorts of requests would bounce off your firewall anyway, but on a live server they become useful for filtering out undesirable IP blocks, known risks, lots of things.

Sometimes, you will only want to ban one IP, perhaps some persistent robot that doesn't play by the rules.

# someone else giving the ruskies a bad name.
order allow,deny
deny from 83.222.23.219
allow from all



There you go. Pretty easy right.

  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Using .htaccess to Rewrite something

Something great here about using .htaccess to rewrite.

Forwarding domain to subdomain using .htaccess

Tell all about how to forward one domain to a subdomain.

Force browser to use https protocol

Add following snippet to your htaccess and redirect entire website to https. RewriteEngine...

Rewrite URLs Using .htaccess

Rewriting product.php?id=12 to product-12.htmlRewriteEngine onRewriteRule...