What Is a .htaccess File?
A .htaccess file is a plain-text configuration file that Apache, and LiteSpeed in compatibility mode, reads inside a specific folder to override server settings just for that folder and its subfolders, without editing the main server configuration. It commonly handles redirects, URL rewriting, password protection, custom error pages and IP blocking. Nginx does not read .htaccess files at all.
What .htaccess is and where it lives
A .htaccess file is a hidden, plain-text file placed inside a website's folder, most often the root folder such as public_html. Apache checks for one in every folder along the path of a request and applies its rules to that folder and everything below it, without needing a server restart. For Apache to honor it at all, the main server configuration must have AllowOverride All set for that directory, which is already the default on most shared hosting.
Common uses with examples
- Redirecting an old URL to a new one.
- Forcing every visitor onto HTTPS.
- Rewriting URLs into clean, readable paths.
- Password-protecting a folder.
- Blocking specific IP addresses.
- Showing a custom error page instead of the default one.
# Redirect an old page to a new one
Redirect 301 /old-page/ /new-page/
# Force HTTPS
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Custom 404 page
ErrorDocument 404 /not-found.html
# Block one IP address
Require not ip 203.0.113.50
.htaccess vs. Nginx, and a performance note
Nginx has no equivalent feature; all of its rules live in central configuration files that are reloaded by the administrator. That is faster to process because Nginx never has to search for override files, while Apache checks every parent folder on every single request for a .htaccess file, which adds a small amount of overhead that grows with deep folder structures.
Troubleshooting a .htaccess file
- Make sure the file is named exactly
.htaccess, with the leading dot and no extension. - Check that
AllowOverrideis enabled for the folder; otherwise Apache ignores the file silently. - A single syntax mistake can cause a 500 Internal Server Error for the whole site, so edit a copy first and check the Apache error log after changes.
- Rules are read top to bottom and the first matching rewrite or redirect usually wins, so order matters.
Frequently asked questions
What is a .htaccess file used for?
It is used to change Apache's behavior for one specific folder, commonly for redirects, URL rewriting, password protection, custom error pages and blocking visitors by IP address, all without touching the main server configuration.
Why isn't my .htaccess file working?
The most common reasons are that AllowOverride is not enabled for that folder, the file is misnamed, or you are using Nginx, which ignores .htaccess files completely. Check the folder's main configuration and the exact file name first.
Can editing .htaccess break my website?
Yes. A single typo or an unsupported directive can cause a 500 Internal Server Error for every page on the site. Always keep a backup copy of the working file before making changes.
Does Nginx support .htaccess files?
No. Nginx has no .htaccess mechanism at all, so any rewrite or redirect rules written for Apache must be translated into Nginx's own configuration syntax and reloaded by an administrator.
Where do I find or create a .htaccess file?
It usually sits in your site's root folder, often called public_html, and may be hidden by default in an FTP client or file manager. If none exists, you can create a new plain-text file and name it exactly .htaccess.
Does .htaccess slow down a website?
The effect is small but real: Apache checks for a .htaccess file in every parent folder on each request. Very deep folder structures or large rule sets add a little overhead compared to rules placed directly in the main server configuration.
Related topics
- What Is Apache (Apache HTTP Server)? Web Servers
- What Is LiteSpeed? Web Servers
- What Is Nginx? Web Servers
- What Is Shared Hosting? Hosting Basics
- What Is an SSL Certificate? Security & SSL