I’m going mad over Apache .htaccess
I’m trying to setting as protected my subfolders using relative address, but it seems impossible.
The path of Apache folder is structured like this:
/var/www/apachedir
now I want to protect
/var/www/apachedir/subfolder/*
What I tryied is putting in /var/www/apachedir/
an .htaccess
file like this
<FilesMatch "subfolder/.*">
Order Allow,Deny
Deny from all
</FilesMatch>
but it seems not woking good.
I don’t want to use ModRewrite
and I want to make this .htaccess
reusable.
So, listen, if I put the site over an other server that has a direcory structure like /var/www/zzz
it has to protect files in /var/www/zzz/subfolder/*
.
Also the file .htaccess
has to stay in the root folder /var/www/apachedir
.
There’s a way to do it?
Edit:
I don’t want to use ModRewrite
but also I don’t want to use Redirectmatch
.
I want to know if there’s a way to set it up with FilesMatch
without ModRewrite
or Redirectmatch
.
2
Answers
You can use
RedirectMatch
to block access to a known path:No, because the
FilesMatch
(and the non-regexFiles
) directive(s) literally match against files only, not directories. eg.<Files "*.jpg">
matches all.jpg
files in any subdirectory.There are various methods to block access to that subdirectory…
Use a
<Directory>
section in the server configIf you have access to the server (virtual host) config then you can use the
<Directory>
(and<DirectoryMatch>
) directive(s) to target specific directories. But this is not permitted in.htaccess
. For example:Create an additional
.htaccess
file in that subdirectoryThe equivalent userland
.htaccess
way of doing this is to create an additional.htaccess
file in that subdirectory (ie. at/subfolder/.htaccess
) with a singleRequire all denied
directive. The.htaccess
file itself is equivalent to the<Directory>
directive in the server config.Aside:
Order
,Deny
andAllow
are Apache 2.2 directives and formerly deprecated on Apache 2.4 (which you are far more likely to be using). You should be using the equivalentRequire
(mod_authz_core) directives instead, as used above.Use
Redirect 403
(mod_alias) – not a "redirect"RedirectMatch
(andRedirect
) are part of mod_alias – this is a base module and compiled into Apache by default (unlike mod_rewrite), so using the prefix-matchingRedirect
directive (no need for the regex variantRedirectMatch
) is a reasonable solution as @anubhava suggests in his answer, depending on the scenario and existing directives. For example:Despite the use of the
Redirect
directive, this is not an external (HTTP) redirect. The 403 response is served via an internal subrequest.Set an environment variable and check with mod_authz_….
Alternatively, you can set an environment variable when the
/subfolder
is requested (usingSetEnvIf
) and check for this using theRequire
directive. This allows you to keep the condition separate from the directives that actually permit access. For example (using Apache 2.4 mod_authz_core):NB: If you are doing any URL-rewriting with mod_rewrite then you might need to check for
REDIRECT_BLOCK_ACCESS
instead in the aboveRequire
directive.<If>
expression (Apache 2.4)On Apache 2.4 you can also use an
<If>
expression to target that specific subfolder with a containing mod_authz_core directive. For example:Although, strictly speaking, these methods target the URL-path, not the file-path.