Understanding .htacess

What is .htaccess?

.htaccess is a configuration file for use on web servers running the Apache Web Server software. When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server’, then the .htaccess file is detected and executed by the Apache Web Server software. These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.

via – http://www.htaccess-guide.com/

Examples

Testing for Support

[crayon]# This Directive will make Apache look first
# for “index_good.html” before looking for “index.html”
DirectoryIndex index_good.html[/crayon]

Hide All media

[crayon]# Disable Directory Listings in this Directory and Subdirectories
# This will hide the files from the public. However you can still
# access files with direct URLs!
Options -Indexes[/crayon]

Hide Some Media

[crayon]# Enable Directory Listings in this Directory and Subdirectories
Options +Indexes

# Fancy Indexing
IndexOptions +FancyIndexing +FoldersFirst +IconsAreLinks

# Ignore png files
IndexIgnore *.png[/crayon]

Using .htaccess to redirect to a mobile directory

This should go in an .htaccess file in the root of your main site directory
[crayon]RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} “android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile” [NC]
RewriteRule ^(.*)$ https://archive.thomaswallace.net/demos/redirect/mobile/ [L,R=302][/crayon]
To avoid looping this needs to go in an .htaccess file in mobile directory
[crayon]RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(stats/|missing\.html|failed_auth\.html|error/).* [NC]
RewriteRule .* – [L]

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* – [L][/crayon]

Resources