I am trying to block access to a specific folder and all of its content. I use methods suggested on the Internet, but they do not work.

This method does nothing:

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

And this one brings internal server error:

<Directory "/folder_name/">
deny from all

What is wrong here?

My htaccess file looks like this:

RewriteEngine on
Options -Indexes
DirectoryIndex index.html

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R,L]

#rewrite rules for base URLs
RewriteRule ^([^/\.]+)[/]*$ $1\.html [QSA,L]
  • 1
    Aparently, you are not supposed to use Directory directive in htaccess, but interesting why rewrite rule does not work. Sep 9, 2020 at 18:26

2 Answers 2

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

This blocks access to the folder only; not its contents (because of the trailing end-of-string anchor). And since you have existing mod_rewrite directives, it will need to go near the top, before the existing directives.

Generally, you should have blocking directives first.

RewriteRule ^folder_name - [F]

: remaining directives follow

Alternatively, you create another .htaccess file inside the directory you want to block, with a single directive:

Require all denied
  • This is a better answer.
    – gnicko
    Sep 15, 2020 at 19:18

I found a method that works:

RedirectMatch 404 ^/folder_name/.*$
  • But note that the existing mod_rewrite directives are still processed before this mod_alias directive is processed, regardless of the order of the directives, so any redirects will still happen.
    – MrWhite
    Sep 9, 2020 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.