Docs > Example of a .htaccess file

What is a .htaccess File?

.htaccess stands for “Hypertext Access” and is a configuration file used on Apache web servers. This file is primarily used for controlling settings at the directory level and allows web administrators to change the server configuration without needing direct access to the main server configuration files.

Key Functions of a .htaccess File

  1. URL Rewriting: One of the most common uses is rewriting URLs, for example, redirecting from old URLs to new ones or removing “www” from URLs to avoid SEO issues.

  2. Security Settings: .htaccess can be used to restrict access to specific areas of your website by blocking IP addresses or requiring passwords for certain directories.

  3. Customizing Error Pages: You can customize which page is displayed when a user encounters an error such as 404 (Page Not Found) or 403 (Forbidden).

  4. Performance Optimization: By setting caching rules and compression, the loading speed of the website can be improved.

  5. Defining MIME Types: You can set specific MIME types for different file extensions, which helps control how the browser processes these files.

Precautions and Best Practices

  • Performance: Although .htaccess is very powerful, its use can impact server performance, as the file is read with every request. In environments where the server configuration can be directly changed, it is often better to make settings directly in the main server configuration file (httpd.conf).

  • Security: Since .htaccess files are so powerful, you should ensure they are kept secure and not publicly accessible. Also, ensure that only trusted users have access to these files.

  • Error Checking: Errors in the .htaccess file can make your website or parts of it inaccessible. It’s important to thoroughly test changes before they go live.

Example

This .htaccess file contains instructions for character set definition, caching, error handling, and URL rewriting. Here is an explanation of each part:

1. Character Set Definition

AddDefaultCharset UTF-8

This command sets the default character set for documents to UTF-8. This helps prevent character encoding problems and ensures that texts are displayed correctly.

2. Caching Settings

ExpiresActive on 
AddType image/webp .webp
AddType image/svg .svg
ExpiresByType image/webp "access plus 365 days"
ExpiresByType image/* "access plus 365 days"
ExpiresByType application/javascript "access plus 365 days"
ExpiresByType text/css "access plus 365 days"
  • ExpiresActive on activates the use of expiry dates for file types.
  • AddType defines new MIME types for WebP and SVG files so that the browser interprets them correctly.
  • ExpiresByType sets the expiry date for various file types (images, JavaScript, CSS) to 365 days after the last access. This improves load times for returning visitors by caching resources in the browser.

3. Error Document

ErrorDocument 404 https://autoprofis24.de/

Specifies that visitors are redirected to the given URL in case of a 404 error (Page Not Found).

4. URL Rewriting

RewriteEngine On

Activates URL rewriting.

Specific Redirections

RewriteRule ^infos/arten-von-hybridautos/(.*)$ /infos/arten-von-hybridfahrzeugen/$1 [R=301,L]
RewriteRule ^blog/brummende-geraeuche-hinterachse-mercedes-e-klasse-differenzial/(.*)$ /blog/brummende-geräusche-hinterachse-mercedes-e-klasse-differenzial/$1 [R=301,L]

These rules redirect old URLs to their new paths (301 redirection, permanent). This is used to transfer SEO value to the new URL and improve user experience.

Checking for Existing Files or Directories

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

These conditions ensure that the following rewrite rules are only applied if the requested file or directory does not exist.

Commented Code for PageSpeed Optimizations

The blocked code (##) is for specific performance settings that are currently not active, such as excluding certain file types from redirection or adding a slash at the end of the URL.

Redirection to a Parent Directory

RewriteRule ^(.+)/[^/]+/?$ /$1/ [R=301,L]

Redirects requests pointing to a non-existing resource in a subdirectory to the parent directory.

Removing “www” from the URL

RewriteCond %{HTTP_HOST} ^www\.autoprofis24\.de [NC

]
RewriteRule ^(.*)$ https://autoprofis24.de/$1 [L,R=301]

Redirects all requests from www.autoprofis24.de to autoprofis24.de, removing the www part to ensure consistency in URL structure and avoid duplicate content.

This .htaccess file is a good example of how server-side configuration can be managed to enhance functionality and security on your website.