Block directory access using htaccess 404

RewriteRule in Apache .htaccess is a convenient way to block directory access to a directory in your Apache document root. This can be useful to avoid accidental access to some content in these directories. Here is the config you can use in .htaccess in the desired directory.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*  - [R=404,L]

Note that you don’t need to restart apache after .htaccess changes. R=404 will set the HTTP status code to 404. L is used to stop processing more mod_rewrite rules.

Here is how the a page having blocked directory looks like in Chrome. We are using directory _internal just for the purpose of this tutorial.

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments