What Is .htaccess?
.htaccess (Hypertext Access) is a per-directory Apache configuration file that lets you override server settings without editing the main httpd.conf. It's most commonly used for URL rewriting, redirects, access control and security headers.
The name starts with a dot, so on Unix-like systems it's hidden by default. In Apache's config, the directive AllowOverride controls whether .htaccess files are honored — often set to All on shared hosting and None on managed servers.
RewriteRule Anatomy
RewriteRule Pattern Substitution [Flags]
- Pattern: a regular expression matched against the URL path without the leading slash.
- Substitution: the target path or URL, with
$1,$2, … backreferences from the pattern. - Flags: modifiers in
[square brackets]— e.g.[R=301,L]for a permanent redirect + stop processing.
RewriteCond Anatomy
RewriteCond TestString CondPattern [Flags]
Applies to the next RewriteRule. Multiple RewriteCond lines above a rule are ANDed together by default, or ORed if you append [OR].
Path-Matching Rules in .htaccess
The URL matched by a RewriteRule in .htaccess does not include the leading slash. So for a request to /blog/post-1 at the web root, the pattern matches against blog/post-1. If your pattern starts with ^/, it will never match in .htaccess context (that syntax is for VirtualHost context).
Common Pitfalls
- Redirect loops: the HTTPS redirect rule matching the HTTPS request itself. Fix with
RewriteCond %{HTTPS} off. - Missing
[L]flag: the next rule overwrites your substitution. - Missing
RewriteEngine On: rules silently do nothing. - Forgetting that files/directories are checked first: use
-fand-dconditions to skip rewrites for real files. - Case sensitivity: URL paths are case-sensitive — add
[NC]if you need case-insensitive matching. - Query string loss: a rule without
[QSA]drops the original query string by default.
Debugging Tips
- Enable rewrite logging on the server:
LogLevel alert rewrite:trace3. - Test each rule in isolation before layering them.
- Use this tester to visualize what happens — but always verify on a staging Apache before pushing to production.
⚠️ AI-Generated Tool Notice: This tester was built with AI assistance. It simulates the most common subset of mod_rewrite behavior — but Apache is complex. Edge cases involving [P], [C], [PT], [NS], internal sub-requests and per-directory contexts are not fully simulated. Always verify critical redirects on a real staging server.