The .htaccess file is a configuration file used on Apache-based web servers. It allows you to control how a website behaves without modifying global server settings.
It is commonly used in hosting environments to define rules for redirects, security, performance, and URL structure.
Table of Contents
What is an .htaccess file?
Originally, the .htaccess file was designed to control access to specific directories on a server.
Today, it is mainly used to:
- override default server settings
- define website rules for URL rewriting
- configure redirects and security behavior
- adjust PHP-related parameters (when supported)
The rules inside the file apply to the directory where it is located and all its subdirectories unless overridden by another .htaccess file further down the structure.
Why does the file start with a dot?
On Linux-based servers, files that start with a dot are hidden by default.
This means:
.htaccessmay not be visible in FTP clients- you may need to enable “show hidden files” in tools like FileZilla
- the file still exists even if it is not displayed
What is .htaccess most commonly used for?
1. Directory protection
It can be used together with .htpasswd to restrict access to specific folders and protect them with authentication.
2. Redirects
It is commonly used to create redirects between URLs or domains.
Example:
Redirect 301 /old-page /new-page
3. PHP configuration adjustments
Depending on server configuration, it may be used to adjust:
- memory limits
- file upload size
- script execution time
4. Caching rules
It can define rules that enable caching mechanisms such as LiteSpeed Cache when supported by the environment.
5. Error logging and debugging
It can be used to enable PHP error reporting and store logs for troubleshooting purposes.
6. HTTPS redirection
It can force all traffic to use HTTPS, ensuring secure connections:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
7. Directory listing protection
It can prevent users from viewing file lists in a directory when no index file is present.
Practical implications
- Changes in
.htaccesstake effect immediately - Incorrect rules can break website functionality
- Rules apply recursively to subdirectories unless overridden
- It is a powerful but sensitive configuration file
Summary
The .htaccess file is a server configuration file used to control redirects, security rules, and website behavior on Apache-based hosting. It applies to directories and their subdirectories and is commonly used for SEO-friendly URLs, HTTPS enforcement, and access control.