human human - 1 month ago 9
Apache Configuration Question

Denying access to files except if there is a specific querystring parameter defined

I have this .htaccess file within a folder which contains subfolders. I have currently disabled the ability to directly access the .zip files within those subfolders, like this:

Options -Indexes
deny from all
<FilesMatch '\.(jpg|jpeg|png|gif|mp3|ogg)$'>
Order Allow,Deny
Allow from all
</FilesMatch>


The thing is that I want to allow those .zip files to be accessed only if there is this
?access=allow
on the querystring.

I tried adding the following at the end of the file:

RewriteCond %{QUERY_STRING} (?:^|&)(access=allow)(?:&|$) [NC]
RewriteRule ^(.*)$ - [R=404,L,NC]


It simply didn't work.

How shall I do this ?

Answer

You can use this rule as your very first rule:

RewriteEngine On

RewriteCond %{THE_REQUEST} \.(jpe?g|png|gif|mp3|ogg)[?\s] [NC]
RewriteRule ^ - [L]

RewriteCond %{QUERY_STRING} (?:^|&)access=allow(?:&|$) [NC]
RewriteRule \.zip$ - [L,NC]

RewriteRule ^ - [F]
Comments