.htaccess is a file that allows directory-level configuration on the Apache web server. Redirects, access rules, cache headers and URL rewriting — all can be managed from this small file. This guide explains what .htaccess does and its practical usage.
Related reading: Virtual host configuration · Linux file permissions
What Is .htaccess?
.htaccess (hypertext access) is a text file that lets Apache apply special configuration to a directory and its subdirectories. Users without access to the main server configuration — for example on shared hosting — adjust site behavior with .htaccess.
The file is placed in the directory it belongs to, and Apache reads it on every request. This flexibility is an advantage; however, reading the file on every request introduces a small performance cost.
What Does .htaccess Do?
- Redirects: Redirecting old URLs to new ones, HTTP to HTTPS, the non-www address to www.
- URL rewriting: Making ugly URLs clean and readable with
mod_rewrite. - Access control: Blocking specific IPs or password-protecting a directory.
- Caching and compression: Defining browser cache duration and compression.
- Custom error pages: Showing your own page for errors like 404 and 500.
Commonly Used .htaccess Rules
Below, the two most common rules — an HTTPS redirect and a custom 404 page — are shown as examples:
# Redirect HTTP to HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Custom 404 page
ErrorDocument 404 /404.html
Why Is There No .htaccess in Nginx?
Nginx does not support .htaccess — and this is a deliberate design choice. Apache searches directory by directory for .htaccess files on every request; Nginx reads the entire configuration once at startup. This difference is one of the reasons for Nginx's performance advantage. In Nginx, the same jobs are done centrally in the server block configuration.
Performance Note
If you have access to the main server configuration, writing rules directly into the virtual host configuration instead of .htaccess is more performant — Apache then does not search directory by directory. .htaccess exists mainly for shared hosting scenarios where the main configuration is inaccessible.
Frequently Asked Questions
Do I need to restart the server for an .htaccess change?
No. Apache re-reads .htaccess on every request; the change takes effect the moment it is saved. This is the most practical aspect of .htaccess.
Why is the .htaccess file not visible?
Files whose name starts with a dot are hidden on Linux. You can view it with the ls -la command or the "show hidden files" option in a file manager.
.htaccess is not working, why?
In Apache, if the AllowOverride directive is None, .htaccess is ignored; the main configuration must have AllowOverride All. Also make sure the mod_rewrite module is enabled.
Manage your site however you like with .htaccess support and correct Apache configuration on KEYDAL hosting. Explore KEYDAL hosting