Web development / Reference guide
Apache mod_rewrite: a practical starting guide
Understand the path a rewrite rule sees, choose between a redirect and an internal rewrite, and check query strings and loops before publishing.
WebForgers ยท
A new guide based on the Apache HTTP Server 2.4 documentation, reviewed on 19 November 2026. The examples use a hypothetical application; its PHP files must exist on your own Apache server.
A rewrite rule is a small routing decision. Write down the incoming path, the intended destination, and whether the visitor should see a new address. These three facts are more useful than starting with a catch-all regular expression.
For a simple move, Apache's guidance on when to avoid mod_rewrite recommends considering simpler tools such as Redirect in mod_alias. Use rewriting when the conditions or mapping actually need it. This tutorial explains Apache configuration; the examples are not instructions to enable PHP on a static website.
Choose the configuration context first
The examples below belong in a .htaccess file at the document root. They assume Apache 2.4, mod_rewrite enabled, and permission to use the directives. A host can disable overrides; adding a file then does not make the rules active. Ask the server administrator to confirm the allowed configuration. Apache describes the prerequisites in its per-directory guide.
Context changes what the pattern receives. In a document-root .htaccess, the path /garments/AB-12 is matched as garments/AB-12. In server or virtual-host context, the URL-path starts with /. In a subdirectory's .htaccess, that directory prefix is removed too. Do not move a pattern between contexts unchanged.
The Apache introduction explains the pattern, replacement and optional flags in a rule. A RewriteCond adds a condition to the following rule. Start with an anchored pattern for one known path; widen it only when the application requires a family of paths.
Move a URL: let the client make a new request
RewriteEngine On
RewriteRule ^old-size-guide$ /size-guide [R=302,L]This example sends /old-size-guide to /size-guide with a temporary 302. It does not match /old-size-guide/ or other paths. The browser receives a Location header and makes another request. Keep the destination available before enabling the rule.
Use a temporary response while validating a proposed move. For an intentional permanent move, change that new rule to R=301 after checking the destination and cache implications. This is not a reason to alter unrelated existing redirects. Record their status and Location before adding a rule. Apache documents the R flag and explains how cached 301 responses can complicate testing.
An internal rewrite keeps the requested address
RewriteEngine On
RewriteRule ^garments/([A-Za-z0-9-]+)$ product.php?sku=$1 [END]A request for /garments/AB-12 is handled by product.php?sku=AB-12 inside the server. This rule itself does not send an HTTP redirect. The application decides the final response: a matching rule alone does not guarantee 200. A nonexistent product should still return a proper not-found response.
The capture accepts only letters, digits and hyphens. That is a deliberately narrow example, not a general encoding strategy for arbitrary user input. The application must validate the SKU again. Apache's RewriteRule reference describes substitution and backreferences.
Choose a query-string policy explicitly
For the internal example, the replacement introduces ?sku=, so the original query is replaced by default. If the application genuinely needs both sets of parameters, [END,QSA] appends the incoming query. Do not append it mechanically: repeated keys can affect application behavior.
When the replacement has no query, an incoming query is normally carried forward. To discard it on this particular move, use a separate variant of the example:
RewriteEngine On
RewriteRule ^old-size-guide$ /size-guide [R=302,L,QSD]The QSA and QSD documentation describes these differences. Choose one policy from the requirements; test a URL with a query as well as a plain URL.
Avoid loops and preserve real files
In per-directory processing, L stops the current pass; an internal rewrite can start another pass. END also stops subsequent per-directory rewriting for the current request. That distinction matters when the replacement could match the same rules again. It does not fix a separate client-side loop such as one redirect from A to B and another from B to A. See Apache's loop discussion.
A front-controller example excludes existing files and directories so that an image or stylesheet can be served directly:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [END]Here index.php must exist and the application must route requests and return suitable statuses. This is an illustrative alternative configuration, not a block to paste underneath every earlier example. Rule order is part of the design.
Check headers and the final response
apachectl -t
curl -I https://shop.example/old-size-guide
curl -L --max-redirs 5 -o /dev/null -w "%{http_code} %{url_effective}\n" https://shop.example/old-size-guideThe example hostname is reserved for illustration; replace it with your test host. apachectl's config test checks configuration syntax, not the behavior of every request. Request the affected paths afterward; on hosts that read .htaccess per request, those requests are essential.
- Inspect the first status and Location without following redirects.
- Follow with a small redirect limit. Check that the final page has the intended content, not just a 200 status.
- Try an unknown path, a real asset, a query string, and the intended slash variant.
- Compare existing rules with the recorded baseline. A broad new match must not capture their paths.
When more detail is needed, Apache provides per-module rewrite logging. Enable a suitable trace level only for diagnosis and return to the normal level afterward; the logging documentation warns about high trace levels.
A quick guide to keep beside the configuration

For the document that the server returns, continue with Build the page in the order people meet it.