Im trying to migrate my .htaccess file from apache 2.2 to apache 2.4, but i'm having a hard time recreating these deny/allow rules.
#deny access to all files inside folders
SetEnvIf Request_URI "^/.+\.[a-z]+$" deny
SetEnvIf Request_URI "(\.gitignore|\.htaccess)" deny
# set acces to public accessible files in main admin folder
SetEnvIf Request_URI "^/admin/(index\.php|tmp)" allow
# set acces to public accessible files in module folders Admin and Site
SetEnvIf Request_URI "^/modules/[^/]+/(site/([^/]+/)?|admin/)(css|js|plugins|images|files|cache|fonts)/" allow
# set acces to public accessible files in uploads folder
SetEnvIf Request_URI "^/uploads" allow
SetEnvIf Request_URI "^/(.*\.xml|.*\.pdf|.*\.js|.*\.htm|.*\.asp|.*\.html|favicon\.ico|robots\.txt|index\.php|communicationlog\.png|sitemap-?([a-z]+)?\.xml\.?(gz)?$)" allow
Order deny,allow
Deny from env=deny
Allow from env=allow
These are the rules im currently struggling with. It seems very hard to recreate these with the <Require>, <RequireAll> and <RequireAny> directives. Can anyone help us out?
I tried just adding these rule with a <RequireAll> and <RequireAny> directives, but these basically deny the entire site and does not reproduce the same results.
Instead of having two variables
denyandallow, I would have just one "allow" var, which is then unset (ie.!allow) as required, instead of settingdeny. This "allow" variable should be set by default (inline with theOrder deny,allowdirective that defaults toallowif neither is set).By having just one variable allows you to grant access with a single
Requiredirective.I would also call this variable something other than
allow, to avoid any confusion with theallowdirective. For the sake of this example I will call itALLOW_ACCESS(convention dictates all uppercase).So, the rules would then become (
SetEnvIfdirectives are pretty much the same, except for the additional "initialisation" one and use of env var):Note that since there is now just one "variable" to grant access, the order of the directives is now significant. Although they already happened to be in the correct order.
If you have any other (deprecated)
Order,AllowandDenydirectives then they also need to be converted at the same time, to avoid any unexpected conflicts.Aside:
It looks like these two directives could be combined:
Which effectively blocks all dot-files (not just
.gitignoreand.htaccess), but that is usually desirable.