Pikkola .htaccess, tool No. 93

Rules you can read, and a URL you can walk through them.

Build the HTTPS and www normalisation, the moved paths, the trailing slash, the cache headers and the security headers, then send a URL through the same rules and see which line catches it. The first match wins, and the page shows where it stopped.

6
rule groups
5
security headers
0
files touched

The rules bench

One file, one order, one outcome

An .htaccess is applied top to bottom and a [L] flag stops the walk. That is the whole mental model, and the simulator uses the same order as the text so a rule that looks right is the rule that runs.

One address
Paths
Cache
Headers

.htaccess

    

Three ideas that decide the outcome

Most broken .htaccess files are not wrong, they are out of order. The rules are applied in the order they appear and the first terminal one wins.

01

Order is the logic

Canonicalise the scheme and the host first, then redirect moved paths, then the cosmetic rules. A trailing slash rule above a redirect will rewrite the path the redirect was written for, and the redirect never fires.

02

Flags say where to stop

[L] stops the current round of rewriting, [R=301] sends a redirect instead of serving, [F] forbids. Without a flag the engine may keep going, which is how a rule that looks harmless produces a loop between two rewrites.

03

A redirect is a promise to other people

A 301 is cached by browsers and search engines, often for months. Use 302 while a move is still being decided, and only write a 301 when the old path will never come back, because you cannot call it back.

What the file does, and what it cannot know

The builder writes directives with their IfModule guard so a server without the module ignores the block instead of failing the request.

Part What this page does What it will not do
The scheme and the host RewriteCond on %{HTTPS} and %{HTTP_HOST}, with a 301 to the canonical form Redirect a POST to https with a 301, which drops the body: use 307 or 308 there
Moved paths Redirect for a prefix, RewriteRule for a pattern with captures Guess whether the target exists: a redirect does not check
The filesystem !-f and !-d guards on the single page fallback Know what is on your disk: the simulator assumes nothing but the entry file
Cache ExpiresDefault for assets, max-age 0 and revalidate for HTML Cache HTML for a year because the asset line said so
Headers Written inside IfModule mod_headers.c Replace a policy set by the application or the CDN
Output A text file you copy or download, with the order visible Upload it anywhere: it is a file to review, not a deployment

Apache reads .htaccess on every request, and a directory full of them costs more than a block in the server configuration. If you can edit the vhost, put these rules there; .htaccess is for the hosting you do not control.

Field guide

Order, flags and the redirect you cannot undo

An .htaccess file is a program with no variables and one control flow: top to bottom, stop when a rule says to. Most files that misbehave are not wrong in their lines but in their sequence.

The order is the logic

Apache applies directives in the order they appear, and a rule with [L] ends the current round of rewriting. That makes the file's order an algorithm: canonicalise the scheme and the host first, because every later rule should see one address; then redirect moved paths; then the cosmetic rules. A trailing slash rule placed above a redirect rewrites the very path the redirect was written for, so the redirect has nothing left to match and silently never fires. That single ordering mistake accounts for a large share of "my redirect does not work" reports.

Two rewrites that turn A into B and B into A are an infinite loop, and no flag fixes it: [L] ends a round, not the request. The fix is a condition that excludes the target, which is why every good rewrite file has a RewriteCond or two above its catch-all.

What each flag actually does

[R=301] sends a redirect rather than serving, and [L] stops processing the current set. [F] returns 403 and [NC] makes a pattern case-insensitive. [QSA] appends the query string, which you need when the substitution has no ? of its own: without it a redirect to /new drops ?page=2.

A 301 on a POST is a trap: most clients follow it with a GET and the body is gone. Use 307 or 308 when the method has to survive, which is why this page offers those codes in the redirect row and not only 301 and 302.

Canonical host and scheme, without breaking anything

The pair of rules at the top of the generated file is the standard shape: RewriteCond %{HTTPS} !=on with a redirect to https://%{HTTP_HOST}%{REQUEST_URI}, then a host rule. The host rule matters for SEO more than for users: two hostnames serving the same page split their ranking, and a cookie set on www is not sent to the bare domain. Pick one, redirect the other, and make the choice in a place that does not depend on the visitor.

Behind a proxy or a CDN, %{HTTPS} can be off on a request that arrived over TLS, because the TLS ended at the proxy. Then the rule loops: the redirect goes to https, the proxy forwards it as http again. The usual fix is a check on X-Forwarded-Proto with a condition, and an awareness that the header is only trustworthy from your own proxy.

The filesystem is a fact the file cannot see

RewriteCond %{REQUEST_FILENAME} !-f asks the server whether a file exists at the requested path. A simulator without your disk cannot answer that, so this page's simulator assumes no file exists except the entry file, and treats a path with a file extension as a file. That assumption is stated on the page rather than hidden, because a simulator that pretends to know is worse than one that says what it assumed.

The same limitation applies to directory checks and to Options -Indexes: the directive is correct, and whether it takes effect depends on AllowOverride, which is a server setting neither the file nor this page can see.

Cache and headers, where a mistake lasts

ExpiresDefault applies to everything a type rule does not name, which is how an HTML page ends up cached for a year: someone set a year for assets and forgot that HTML was covered too. The generated file sets max-age=0, must-revalidate on HTML explicitly and a long immutable cache only on hashed assets, which is the arrangement that lets a deploy take effect on the next request.

Header rules in .htaccess can conflict with the application and with a CDN in front of it. Header always set wins over Header set and is the one that survives an error response, but if the CDN also sets the header, the browser may see two. Check the deployed response with curl rather than trusting the file.

The same rules in nginx syntax are on Pikkola nginx, and the status codes a redirect sends are on Pikkola HTTP.

Questions

Will this work on nginx?

No. nginx has no .htaccess and no per directory overrides; the same intent is a server block with return and try_files directives. The rules here are Apache syntax, and copying them into nginx gives a 404 at best. If you are on nginx, the shape of the logic still helps, the lines do not.

Why did my redirect loop?

Most often because a rewrite changes the path into something an earlier rule matches again, so the URL bounces. Add a condition that excludes the target, or make sure the redirect rule is above the rewrite that produces the same path, and add [L] so the round stops. Two rules that each turn A into B and B into A will loop forever and no flag saves that.

Is a 301 always better than a 302?

Only when the move is permanent. A 301 is cached by the browser and by search engines, and reverting it means waiting for those caches to expire. For an experiment, a temporary outage, a seasonal page or a move still being debated, a 302 keeps the option open at the cost of a little ranking signal.

Why is the trailing slash rule before the single page fallback?

Because the fallback serves everything that is not a file, and a path with a trailing slash is not a file, so the fallback would swallow it and the canonical URL would never be reached. Removing the slash first means there is one URL per page, which is the point of the rule.

Does the simulator prove my server behaves this way?

No. It proves the order and the patterns in the file you generated are consistent, which is the part people get wrong. Whether a request reaches the file at all depends on AllowOverride, on the vhost and on the CDN in front. Test the deployed server with curl and read the Location header, which is the only authority.