Useful .htaccess hacks

Once in a while I have to perform some htaccess hacks. This list is kind of a public notepad for htaccess rules I sometimes use.

If multiple domains (or subdomains) point to the same content, this content might be considered as duplicate content by search engine providers (e.g., Google, Bing, Yahoo!). To prevent this, you should choose one (sub)domain and redirect all other (sub)domains to the chosen one. Imagine you have example.com, www.example.com, example.org and www.example.org. Now you decide to redirect all requests to www.example.org:

RewriteEngine On
RewriteCond %{http_host} !^www\.example\.org [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [L,R=301]

The nice thing about this rule is, that you don’t need to specify all available domains. Thus, it also works with wildcard subdomains.

If a folder should not be accessed by anyone (e.g., because its content is only read by server-side scripts), it is highly recommended that you block all access attempts:

Order deny,allow
Deny from all