Search code examples
apache.htaccessmod-rewritehttpd.conf

Apache HTTPD - recursively deny specific file extensions and/or subdirs?


I have an Apache 2.4 web server, and in its htdocs folder, there is a complicated structure of directories and subdirectories.

Is it possible to create a single, top-level .htaccess file that would deny specific file extensions (e.g. .bak) and/or traversing into specific directory names (e.g. .git) for the whole subdirectory structure?

E.g., I would like to respond with HTTP 403 Forbidden when requested:

htdocs/index.html.bak
htdocs/aaa/bbb/ccc/login.php.bak
htdocs/foooooooo/.git/subdir/some_pic.png

If not via .htaccess, then perhaps this can be achieved via httpd.conf?

Thanks!


Solution

  • Thanks! I ended up with the following .htaccess:

    RewriteEngine On
    RewriteRule \.bak$          - [F]
    RewriteRule (^|/)\.git($|/) - [F]
    

    Of course, this line in httpd.conf has to be uncommented first:

    LoadModule rewrite_module modules/mod_rewrite.so