BLOG

.htaccess Sheet

26 Apr 2011, Posted by BoB in All Posts, Website Talk

Disable directory listing:
IndexIgnore *
-or-
Options -Indexes

Change default “index” page:
DirectoryIndex anyfilename.html

Custom 404 page redirect:
# 404 error redirect #
ErrorDocument 404 /full/directory/path/404.html

Hotlinking protection:
# hotlink protection #
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://domain.com/.*$ [NC] RewriteCond %{HTTP_REFERER} !^http://www.domain.com/.*$ [NC] RewriteRule .*\.(gif|jpg|png|ico)$ – [F,L]

1 hour web browser caching (improves site performance):
# default expires set to 1 hour #
ExpiresActive On
ExpiresDefault A3600
# 1 hour media file caching #
ExpiresDefault A3600
Header append Cache-Control “public”
# 1 hour commonly updated file caching #
ExpiresDefault A3600
Header append Cache-Control “private, must-revalidate”

Require WWW:
# http://url.com to www.url.com redirect #
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI}

Require HTTPS:
# http://www.url.com to https://www.url.com redirect #
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Subdomain redirect:
# subdomain redirect #
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain1.domain.com$ [OR] RewriteCond %{HTTP_HOST} ^subdomain2.domain.com$
RewriteRule ^/?$ “http\:\/\/alt-subdomain\.domain\.com\/alternate\/path\/” [R=301,L]

Specific file redirect:
# file re-direct #
RewriteEngine On
RewriteCond %{THE_REQUEST} ^.*/index.(html|aspx|htm)
RewriteRule ^(.*)index.(html|aspx|htm)$ http://www.rightpathonline.com/ [R=permanent,L] RewriteCond %{THE_REQUEST} ^.*/default.(php|html|aspx|htm)
RewriteRule ^(.*)default.(php|html|aspx|htm)$ http://www.rightpathonline.com/ [R=permanent,L] RewriteCond %{THE_REQUEST} ^.*/home.(php|html|aspx|htm)
RewriteRule ^(.*)home.(php|html|aspx|htm)$ http://www.rightpathonline.com/ [R=permanent,L]

File compression (improves site performance):
# compression #
<ifModule mod_deflate.c>
<filesMatch “\.(xml|txt|html|js|css|php|cgi|pl|htm|jpg|png|jpeg|ico|swf)$”>
SetOutputFilter DEFLATE
</filesMatch>
</ifModule>

Set default character encoding:
SetEnv LC_ALL en_US.UTF-8

Adv WWW redirect over HHTP:
# HTTP WWW re-direct #
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.bashtechsolutions\.(.*)
RewriteRule (.*) http://www.bashtechsolutions.com%{REQUEST_URI} [R=301,L]

Adv WWW redirect over HHTPS:
# HTTPS WWW re-direct #
RewriteEngine on
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.bashtechsolutions\.(.*)
RewriteRule (.*) https://www.bashtechsolutions.com%{REQUEST_URI} [R=301,L]

Useful websites referencing htaccess:
http://www.addedbytes.com/blog/code/password-protect-a-directory-with-htaccess/
http://www.4webhelp.net/us/password.php
http://webdesign.about.com/od/htaccess/ht/password_1_file.htm

Post a comment