An .htaccess file is a per-directory configuration file for the Apache HTTP Server. It lets you change server behaviour, such as URL rewriting, redirects, HTTPS enforcement and access rules, without editing the main server configuration or restarting Apache. Most of its power comes from mod_rewrite, which matches the requested URL against regular expressions and rewrites or redirects it.
Apache reads .htaccess on every request, so it is convenient on shared hosting but slower than putting the same rules in the main configuration. If you control the server, prefer the virtual host configuration. Nginx and other servers do not read .htaccess at all.
Apache looks for .htaccess in the requested directory and in every parent directory up to the document root. Rules in deeper directories override rules above them. The file is only honoured when the virtual host allows it with the AllowOverride directive.
AllowOverride None makes Apache ignore the file completely, which is also the faster and safer choice when you can move your rules into the virtual host. Rewrite rules need mod_rewrite enabled (a2enmod rewrite on Debian and Ubuntu).
RewriteRule and RewriteCond use Perl-compatible regular expressions. These are the building blocks you need most often.
| Token | Meaning | Example | Matches |
|---|---|---|---|
. | Any single character | c.t | cat, cbt, cct |
+ | One or more of the previous item | a+ | a, aa, aaa |
* | Zero or more of the previous item | (ab)* | empty string, ab, abab |
? | Optional previous item | colou?r | color, colour |
\ | Escapes the next character | \. | a literal dot |
^ | Start of the string | ^a | strings starting with a |
$ | End of the string | a$ | strings ending with a |
( ) | Group, and capture for back-references | (ab)+ | ab, abab, ababab |
[ ] | One character from a set | c[abc]t | cat, cbt, cct |
[^ ] | One character not in the set | c[^a]t | cbt, cct, not cat |
In a RewriteRule inside .htaccess, the pattern is matched against the URL path without the leading slash and without the query string. Captured groups are available as $1, $2 and so on in the substitution. Groups captured in a RewriteCond pattern are available as %1, %2.
Every rewrite block starts by turning the engine on. A rule has three parts: a pattern, a substitution and optional flags in square brackets. Conditions placed directly before a rule must all pass for that rule to apply.
This is the classic front-controller pattern used by CodeIgniter and similar frameworks. The two conditions mean "the request is not an existing file and not an existing directory". Only then is the path passed to index.php.
| Flag | Meaning |
|---|---|
L | Last rule: stop processing further rules in this pass |
QSA | Query string append: keep the original query string |
R=301 | External redirect with the given status code |
NC | Case-insensitive match |
PT | Pass the result through to other URL handlers |
NE | Do not escape special characters in the output |
Use R=301 only for permanent moves, because browsers and search engines cache a 301 aggressively. While testing, use R=302.
If TLS is terminated by a load balancer or CDN, %{HTTPS} is always off on the origin. Test the X-Forwarded-Proto header instead with RewriteCond %{HTTP:X-Forwarded-Proto} !https.
Use only one of the two blocks. Pick one canonical host so search engines do not index duplicates.
For CodeIgniter 4, point the web server at the public folder and use the .htaccess that ships with the framework rather than writing your own.
Without the R flag the browser address bar still shows /image.jpg, because this is an internal rewrite and not a redirect.
Apache 2.4 uses Require all denied. The old Order allow,deny and Deny from all syntax belongs to Apache 2.2 and only works in 2.4 through the compatibility module.
RewriteCond and RewriteRule with R=302 while testing, then switch to 301./a to /a/ must not match its own output.LogLevel alert rewrite:trace3 in the virtual host (not allowed in .htaccess) and watch the rewrite decisions.AllowOverride None once you have server access.Put it in the directory it should control, usually the document root of the site. It applies to that directory and everything below it.
Usually AllowOverride is set to None for that directory, mod_rewrite is not enabled, or the server is Nginx, which never reads the file.
A 301 is permanent and is cached by browsers and search engines. A 302 is temporary and is the safer choice while you test.
Slightly. Apache checks for the file in every directory on the path for each request. The cost is small on most sites, but rules in the main configuration are faster.
.htaccess gives you per-directory control over Apache, and mod_rewrite with regular expressions covers redirects, HTTPS, canonical hosts and framework front controllers. Learn the handful of regex tokens and flags, test with 302 first, read the error log, and move rules into the virtual host when you can.