How to Deny Access to anything except php files in Apache? - apache

I want to deny access to anything except php files. I try with the following code in the httpd.conf file:
<Directory "c:/wamp/www/stf_1">
<Files *.*>
Order Deny, Allow
Deny from All
</Files>
<Files *.php>
Order Allow, Deny
Allow from All
</Files>
</Directory>
After the modification I tried to restart the server but doesnt respond. I don't know what is wrong.

I think the order of the directives is important. The way I am reading it is that you are first denying everything so the second directive doesn't fire.
Try
<Directory "c:/wamp/www/stf_1">
<Files "*.php">
Order Allow, Deny
Allow from All
</Files>
<Files "*.*">
Order Deny, Allow
Deny from All
</Files>
</Directory>

Related

.htacess specifying specific files on one line

I have the following in part of my .htaccess file:
# the following prevents display of the filetohide file
<files handler.php>
order allow,deny
deny from all
</files>
<files submit.php>
order allow,deny
deny from all
</files>
Can multiple file specifiers not be done within a single set of tags, i.e:
<files submit.php|handler.php|otherfile.txt>
order allow,deny
deny from all
</files>
Could you please try following, if this helps you for multiple file names with different formats(I am yet to test this but written this based on regex).
<files ~ "^((submit|handler)\.php)$|^(otherfile\.txt)$">
order allow,deny
deny from all
</files>

Combine Apache commands in .htaccess

If i have two files I want to deny access to on an Apache server - is there a way of combining them instead of writing the same code twice (or more times for other files as well)?
APACHE
<files wp-config.php>
order allow,deny
deny from all
</files>
<Files xmlrpc.php>
Order Deny,Allow
Deny from all
</Files>
For multiple files use FilesMatch like this :
<filesMatch "wp-config(\.php)?|xmlrpc(\.php)?">
order allow,deny
deny from all
</filesMatch>
That will match wp-config.php or wp-config or xmlrpc.php or xmlrpc with extension or not

Is it possible to block web-content (images) for users that don't match one ip?

I have e.g. two pictures:
foobar.png (deny from all except one ip)
foobar_preview.png (allow for every one)
Now I want to deny the content for all clients except one
Now my .htaccess looks like this:
<Files ~ "\.png$">
Order allow,deny
Deny from all
</Files>
<Files ~ "\_preview.png$">
Order allow,deny
Allow from all
</Files>
So I want to allow the "\.png$" files only for one server (so one IP-Adress)
It there a way to realize this?
You can use Allow from IP directive:
<Files ~ "\.png$">
Order allow,deny
Allow from 11.22.33.44
Deny from all
</Files>
<Files ~ "\_preview.png$">
Order allow,deny
Allow from all
</Files>

.htaccess block all in directory exept index.php

I wrote this script to deny all exept index.php
Order allow,deny
Deny from All
<Files *>
Order Deny,Allow
Deny from all
</Files>
<Files index.php>
Order Deny,Allow
Allow from all
</Files>
However links like http://mywebsite.com/image.jpg are still shown. How is that possible?

.htaccess deny all --> directoryindex not working (deny all & whitelisting files)

I would like to deny access to all files and directories on the server but a few ones that I explicitly allow. How can I do that with .htaccess? Why does my approach not work? I am aware I will have to allow .css, .jpg etc.
DirectoryIndex index.html
Order Deny,Allow
Deny from all
Allow from 127.0.0.1
<Files index.html>
order Allow,Deny
Allow from all
</Files>
edit: the above .htaccess gives me a "Forbidden" error when I try to access index.html. why?
edit: this seems to do the trick. I hope there are no holes left:
#Disallow everything
<filesmatch "\.+">
Order Allow,Deny
Deny from all
</filesmatch>
#Allow index
<Files index.html>
order Allow,Deny
Allow from all
</Files>
#Allow peripheral files
<FilesMatch "\.(css|png|jpg|js|ico)$">
Order Allow,Deny
Allow from all
</FilesMatch>
IP address : 127.0.0.1 have access to your server and others don't.
this part:
<Files index.html>
order Allow,Deny
Allow from all
</Files>
set access to index.html for all users BUT remember because you did not mention anything about other files they have default access attributes.
for example the code below allow files: 01.jpeg or 01.html or anything ended with xml.
<FilesMatch !"(01\.jpe?g|01\.html|xml)$">
order Allow,Deny
allow from 127.0.0.1
</FilesMatch>