Navigate
Sponsors
|
Password Protection of FilesIntroduction::As well as protecting directories .htaccess can also be used to protect individual files or types of files. Step 1 - Required Files:As with protecting a directory there are two files you need to
create to protect (and allow limited access to)
your files, .htaccess and .htpasswd. Also the . (dot, period, fullstop) is very important! Step 2 - Creating the .htaccess file:The .htaccess file is created as normal in a text editor. To protect individual files we can use the <FilesMatch> tag.
<FilesMatch secrets.htm> secrets.htm is obviously the name of the file you want to protect and ensure the location of the .htpasswd file is correct. Step 3 - Protecting multiple files:The easiest way to protect a number of files would be to place them all into a separate password protected directory but it is also possible to protect a number of files without protecting the whole directory. As with the above example the same code is used for each file you want to protect.
<FilesMatch secrets.htm> It may be also possible to protect a number of files with the following method:
<Files secretA.htm,secretB.htm,secretC.htm> ...although FilesMatch is the preferred method. Step 3 - Protecting file types:We can protect types of files or files which match a certain criteria by using <FilesMatch>. To protect the common image file types we can use the following:
<FilesMatch "\.(gif|jpe?g|png)$"> To protect just one type of format we can simplify the first line: <FilesMatch "\.(png)$"> Here all files ending in .png are protected. More information on <Files> & <FilesMatch> |