htaccess file protection

Started on the old forum in August 2009 · view the original in the archive · old links redirect here

7 replies · 1,030 views · started 17 years ago · 1 min read

4 participants
ori Lead · 512 posts #1

heres a code i use to protect my gallery files from being accessed outside the site

also the directory listing will not show these file types

.htaccess

<FilesMatch "\.(jpe?g|png|gif|bmp)$">
Order Deny,Allow
Deny from all
ErrorDocument 403 /404.php
</FilesMatch>

this file makes it look like the files dnt exist but its just a 403 forbidden error 404.php

<?
echo "<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL $_SERVER[REQUEST_URI] was not found on this server.</p>
<p>Additionally, a 404 Not Found
error was encountered while trying to use an ErrorDocument to handle the request.</p>
<hr>
$_SERVER[SERVER_SIGNATURE]</body></html>";
?>
anderson Developer · 146 posts #2

Can you explain a bit more? I couldnt understand easily. But i am interested on it. @ ori

Wait...

ori Lead · 512 posts #4

anderson wrote: Can you explain a bit more? I couldnt understand easily. But i am interested on it. @ ori

ok instead of just blocking access to folders this code shows the folder contents but hides the file types put in it and if there smart enough 2 guess the filenames itll come up as 404 not found error instead of 403 forbidden

anderson Developer · 146 posts #5

ori wrote: ok instead of just blocking access to folders this code shows the folder contents but hides the file types put in it and if there smart enough 2 guess the filenames itll come up as 404 not found error instead of 403 forbidden

I cant understand this:

<FilesMatch "\.(jpe?g|png|gif|bmp)$">

if my image file directory is ../images/abcd.gif then what shoud I put in it?

Wait...

ori Lead · 512 posts #6

just put the code in a .htaccess file in ur images folder

anderson Developer · 146 posts #7

yes. I understand now. thanks. I am opening a thread about isipban() function hope you will all visit there.

Wait...

arnage Lead · 811 posts #8

Some more tips...

# secure htaccess file
<Files .htaccess>
 order allow,deny
 deny from all
</Files>
# disable directory browsing
Options All -Indexes
# pass the default character set
AddDefaultCharset utf-8
# disable the server signature
ServerSignature Off
# set the server timezone
SetEnv TZ Europe/Belgrade
# cache images and flash content for one month
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|swf)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
# limit server request methods to GET and PUT
Options -ExecCGI -Indexes -All
RewriteEngine on
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK|OPTIONS|HEAD) RewriteRule .* - [F]
# prevent viewing of a specific file
<files secretfile.jpg>
 order allow,deny
 deny from all
</files>
# serve alternate default index page
DirectoryIndex business.html
# limit access to local area network
<Limit GET POST PUT>
 order deny,allow
 deny from all
 allow from 192.168.0.0/33
</Limit>
# block IP range by CIDR number
<Limit GET POST PUT>
 order allow,deny
 allow from all
 deny from 10.1.0.0/16
 deny from 80.0.0/8
</Limit>
# password-protect single file
<Files secure.php>
AuthType Basic
AuthName "Prompt"
AuthUserFile /home/path/.htpasswd
Require valid-user
</Files>
# password-protect multiple files
<FilesMatch "^(execute|index|secure|insanity|biscuit)*$">
AuthType basic
AuthName "Development"
AuthUserFile /home/path/.htpasswd
Require valid-user
</FilesMatch>

.....

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close