Block directory access with .htaccess rules

It’s very simple to block access to a directory. We will use in this article some rules for the .htaccess file. Just add the following lines in your .htaccess file:

RedirectMatch 403 ^/folder_name/?$
ErrorDocument 403 default

The above lines will block folder access but will permit access to resources inside the folder. To block access to the folder and all its content, use:

RewriteRule ^folder_name/?$ - [F,L]
ErrorDocument 403 default


The rule can help in blocking some server attacks. An example from our Apache logs:

     20 /usr/local/apache/domlogs/getimages.domainname.com:191.5.199.250 "POST /admin/
     20 /usr/local/apache/domlogs/getimages.domainname.com:85.101.157.105 "POST /admin/
     20 /usr/local/apache/domlogs/getimages.domainname.com:91.139.203.119 "POST /admin/
     21 /usr/local/apache/domlogs/getimages.domainname.com:187.120.145.248 "POST /admin/
     21 /usr/local/apache/domlogs/getimages.domainname.com:213.166.150.190 "POST /admin/
     21 /usr/local/apache/domlogs/getimages.domainname.com:77.31.75.180 "POST /admin/
     21 /usr/local/apache/domlogs/getimages.domainname.com:91.93.49.135 "POST /admin/
     22 /usr/local/apache/domlogs/getimages.domainname.com:170.84.172.187 "POST /admin/
     23 /usr/local/apache/domlogs/getimages.domainname.com:123.193.236.74 "POST /admin/
     23 /usr/local/apache/domlogs/getimages.domainname.com:27.111.43.138 "POST /admin/
     23 /usr/local/apache/domlogs/getimages.domainname.com:62.240.2.74 "POST /admin/
     24 /usr/local/apache/domlogs/getimages.domainname.com:178.149.203.99 "POST /admin/
     25 /usr/local/apache/domlogs/getimages.domainname.com:114.157.222.182 "POST /admin/
     25 /usr/local/apache/domlogs/getimages.domainname.com:93.86.254.50 "POST /admin/
    327 /usr/local/apache/domlogs/getimages.domainname.com:195.244.36.34 "POST /admin/

In this case, just put the first code snippet in the /home/.htaccess file. The rules in this file will be applied to all domains on the server.

Related articles:
.htaccess articles

Leave a Reply