.htaccess tips and tricks
If you have a webhosting you have to have basic know how of tips and tricks you can do with htaccess. So here is a list of basic actions that can be done via htaccess and how to use them.
Disable Directory Browsing
Options All -Indexes
Enable Directory Browsing
In the remote case that you want to list your directories:
Options +Indexes
## block a few types of files from showing
IndexIgnore *.wmv *.mp4 *.avi
Customize Error Messages
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
Change Default Page
The pages are searched in the order specified.
DirectoryIndex index.html index.htm index.php fallback.html
Forward all http://www.techmonks.net to http://techmonks.net (Good for removing duplicate links)
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.techmonks\.net$ [NC]
RewriteRule ^(.*)$ http://techmonks.net/$1 [R=301,L]
Redirect to a new URL
Redirect oldlocation.html http://www.techmonks.net/index.html
Redirect /subdamain http://www.techmonks.net/navin
Disable access to images from other websites (Hot linking)
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?techmonks.net/.*$ [NC]
RewriteRule \.(gif|jpg)$ - [F]
You can also replace it with a static/warning image:
RewriteRule \.(gif|jpg)$ http://techmonks.net/warningmessage.jpg [R,L]
Allow only local users
order deny,allow
deny from all
allow from 192.168.0.0/24 (replace with local subnet)
Block Users from accessing the site
order deny,allow
deny from 1.1.1.1 (deny subnet / ip)
deny from .blockeddomain.com
allow from all
Stop .htaccess (a file) from being viewed
order allow,deny
deny from all
Grant CGI (perl) Access in a directory
Options +ExecCGI
AddHandler cgi-script cgi pl
Enable Gzip compression
# BEGIN GZIP
AddOutputFilterByType DEFLATE text/html text/xml text/css application/x-javascript application/javascript
# END GZIP
