htaccess files are directory-specific configuration files for NCSA-compliant web servers such as the Apache Web server. All settings in the .htaccess file apply to the directory where the file was stored, as well as all subdirectories, and enter into force immediately. A restart of the server is not required.

Different uses of the .htaccess file[edit]

Typical uses of the .htaccess file are rewriting and redirecting URLs, administration of error documents and access protection for individual files or entire directories.

Rewriting of a dynamic to a static URL[edit]

Using the Apache module mod-rewrite which is installed and activated on most servers, a dynamic URL can be rewritten in such a way that it looks like a static URL for humans and search engines.

Example .htaccess entry to enforce the appropriate host:

RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*)$ http:// $1 [QSA,L,R=301]

Redirection from an old to a new URL[edit]

Redirecting an old URL to a new URL is an option when there is a domain transfer or redirects need to be done for other reasons (such as relaunch, restructuring). Again, a permanent redirect should be chosen. In the code above, instead of the last line, the following is inserted:

RewriteRule ^alteseite.html$ /neueseite.html [R=301,L]

Redirect because of “www.”[edit]

Many domains can be retrieved with www. or without www. The redirect from “without www.” to “with www.” is important because duplicate content can be avoided that way. Search engines recognize domain names with www. as the default address if the redirect points to this URL. The [NC] in the code below means “case sensitive,” in other words domain names which are capitalized get redirected.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^$ [NC]
RewriteRule ^(.*) http:// /$1 [L,R=301] 

Protection against image theft[edit]

The .htaccess file is a way to prevent a user from downloading an image from a website or inserting it on his own website. This not only safeguards your images from becoming subject to content theft, but also prevents traffic being stolen from the website which provided the image as a resource by the user who has integrated the image on their own website.

The first line of code excludes this rule for your own website, so that calling up an image does not result in an error message, because all images should be displayed correctly on your website. The second line specifies that all external requests for graphic files should cause an error message to be displayed.

RewriteCond %{HTTP_REFERER} !^http:// [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule \.(jpe?g|gif|png)$ - [F]

Importance for SEO[edit]

Rewriting and redirecting of URLs in particular are of importance for search engine optimization. The redirecting status is especially significant from an SEO perspective. If nothing is specified here, most servers interpret this as a temporary redirect and consider it as a 302. If the redirect should be interpreted as a permanent redirect (301), however, it must be explicitly specified because a 301 redirection transfers the ranking of the linked source to the target resource.

A 302 redirect is interpreted by Google differently. The forwarding is considered to be only temporary and the reputation of the linked source is not transferred to the target resource. When redirecting or rewriting URLs, as well as directing to a “www.” resource, a 301 redirect should be chosen.

The decision to redirect to “www.” version if a URL without www. abbreviation is called up is also important because a preferred canonical URL facilitates Google in identifying the default address. The same duplicate content is avoided. In all redirects it is important to ensure that no 404 error codes are output. Google will not usually index sites that display in the server response a 404 error code. A technically incorrect handling of redirects via .htaccess may therefore have major impact on the reputation of a website and sometimes result in the webpage not getting indexed or being downgraded in the rankings.

Web Links[edit]