Skip to main content

.htaccess

Updated: October 30, 2026
By Willya Randika

.htaccess is a per-directory config file that Apache reads to override the main server configuration for one folder and everything under it. It usually sits in the document root and sets up redirects, URL rewrites, access control, and the DirectoryIndex. The cost is real: Apache opens and processes this file on every request to that folder, so many .htaccess files deep in a tree add work per request. It only runs where the server allows it, and one wrong character can take down the whole folder.

Redirect and rewrite rules

The pattern you see most is RewriteEngine On as the switch, RewriteCond as the condition, and RewriteRule as the action. A common force-HTTPS block looks canonical:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

The R=301 flag marks a permanent redirect, the kind covered under Redirect 301 vs 302, while L stops processing at that rule. To collapse a www version into one host for a Canonical URL, add a RewriteCond on HTTP_HOST and point it at the target domain. DirectoryIndex decides which file serves when a folder is opened, usually index.php or index.html.

Blocking and protection rules

A single Require all denied line locks a folder, and Options -Indexes stops Apache from listing a directory's contents. IP blocks and hotlinking prevention are also written here. Because these directives change who can get in, test in a private window so the browser cache does not hide the real result.

Not just Apache

LiteSpeed honors .htaccess when configured to, but Nginx has no concept of this file and needs the rules moved into a server block. When you switch between web servers, rewrite rules have to be translated, not copied. If a redirect does nothing even though the syntax looks right, check whether AllowOverride in the main config permits per-folder overrides; with AllowOverride None Apache ignores .htaccess entirely.

One character, one 500

An unknown directive or a typo makes Apache return an HTTP 500 for everything in that folder, often with no hint about which line broke. The error page just says Internal Server Error, so the cause lives in the error log, not the browser. Keep a .htaccess.bak copy before experimenting, and if the site turns 500 after an edit, rename the file first to isolate the source. WordPress, security plugins, and cache plugins also rewrite this file, so track your manual edits somewhere else.

FAQ

Why does my site show 500 after editing .htaccess?

Usually an unrecognized directive or a syntax typo. Rename the file to .htaccess.bak to confirm, then read the Apache error log to find the offending line.

Does .htaccess work on Nginx?

No. Nginx does not read .htaccess. Redirect and rewrite rules must be moved into the server block configuration.

Can I have several .htaccess files in subfolders?

Yes, and the rules stack hierarchically. More files make it harder to see which rule wins, so keep rules at the root when that is enough.

Disclaimer: Hosting Wiki articles are prepared for educational and reference purposes. Hosting technology keeps evolving, so some technical details may change over time.