What is .htaccess?
The .htaccess is a configuration file for use on web servers running the Apache Web Server software. When a .htaccess file is placed in a directory which is in turn ‘loaded via the Apache Web Server’, then the .htaccess file is detected and executed by the Apache Web Server software. These .htaccess files can be used to alter the configuration of the Apache Web Server software to enable/disable additional functionality and features that the Apache Web Server software has to offer. These facilities include basic redirect functionality, for instance if a 404 file not found error occurs, or for more advanced functions such as content password protection or image hot link prevention.
Think of it like a set of local rules for your website — without changing the main server settings.
Key Points
-
Location: Placed in the root directory (or any subdirectory) of your website.
-
Effect: Rules inside it apply to that directory and its subdirectories.
-
Name: The dot (
.) at the start means it’s a hidden file in Unix/Linux systems.
Common Uses
-
Redirects
-
Example: Redirect
old-page.htmltonew-page.html
-
-
URL Rewriting (Pretty URLs)
-
Often used with
mod_rewriteto remove.phpor make SEO-friendly URLs.
-
-
Password Protection
-
Restrict access to a folder with a
.htpasswdfile.
-
-
Custom Error Pages
-
Caching & Compression
-
Improve site speed by setting browser cache rules and enabling gzip compression.
-
-
Security Restrictions
-
Block specific IPs, disable directory browsing, etc.
-
Why It’s Useful
-
No need to modify the main server configuration (good for shared hosting).
-
Gives developers fine control over site behavior.
-
Can help with SEO, security, and performance.
⚠ Caution:
-
A wrong rule in
.htaccesscan break your site. -
It’s processed on every request, so overly complex rules can slow things down.