Guides
.htaccess Redirects: 301, RewriteCond and Rules That Actually Fire
Most broken .htaccess redirects are not syntax errors. They are rules that never fire because an earlier rule matched first, or conditions that apply to the wrong line.
Starte ein frisches DomainLens-Audit und nutze den Report als Prioritätenliste.
Where .htaccess applies, and where it does not
A .htaccess file is Apache-only, and only honoured when the server is configured with AllowOverride permitting it. On nginx it does nothing whatsoever — a fact worth confirming before spending an afternoon debugging a file the server never reads.
Apache re-reads the file on every request, which is why changes take effect instantly and why heavy use costs measurable performance. If you control the main server configuration, the same rules belong there instead.
Redirect versus RewriteRule
Two modules can redirect, and mixing them is a common source of confusion. Use the simple one when the rule is simple.
- Redirect matches a path prefix, so /old-page also catches /old-page/anything — often not what you intended.
- RewriteRule patterns in .htaccess match the path without the leading slash; in server config they include it.
- Without R=301 you send a 302, and a permanent move announced as temporary never consolidates.
- Without L, processing continues and a later rule can redirect the result again.
# mod_alias — simple, path-prefix based
Redirect 301 /old-page /new-page
RedirectMatch 301 ^/blog/(.*)$ /articles/$1
# mod_rewrite — needed once conditions are involved
RewriteEngine On
RewriteRule ^old-page/?$ /new-page [R=301,L]
# Flags that matter:
# R=301 issue a permanent redirect (default is 302)
# L last — stop processing further rules
# NC case-insensitive match
# QSA append the original query string
RewriteCond applies to one rule only
This is the rule that trips people up most. A RewriteCond applies solely to the single RewriteRule immediately following it. Stacking three conditions above one rule means all three must pass for that rule; putting a blank line or another rule in between silently disconnects them.
Multiple conditions are ANDed by default. Use the [OR] flag when you need either to match.
RewriteEngine On
# Force HTTPS — condition applies to the next rule only
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# Strip www — separate condition, separate rule
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# %1 is the capture from the last RewriteCond
# $1 is the capture from the RewriteRule
# Confusing these two is the classic mistake.
Order matters, and chains accumulate
Rules are evaluated top to bottom and the first match with L wins. Put specific rules above general ones, or the general one will swallow everything before the specific one is reached.
Apache also re-enters the ruleset after a rewrite that does not redirect, which means a rule can match its own output and loop. The L flag stops processing for that pass but not the re-entry, so a rule whose replacement still matches its own pattern will run until Apache gives up and returns a 500. Anchoring patterns with ^ and $ prevents most of this.
Separate rules for HTTPS, for the www variant and for trailing slashes each add a hop. Requesting http://www.example.com/page can travel three redirects before reaching content. Combine them into one rule that jumps straight to the final form.
RewriteEngine On
# Single rule: fixes scheme and host together
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# Verify what you shipped:
# curl -sIL http://www.example.com/page \
# | grep -Ei '^(HTTP|location)'
How DomainLens contributes
DomainLens reports the status code and the full redirect chain for the URLs it audits, so a rule that produces three hops instead of one is visible without reading the file. For collapsing chains see redirect chains , for application-level redirects see redirects in PHP, JS and Django , and to trace a live URL use the redirect checker .
- Why is my .htaccess rule not working?
- Most often an earlier rule matched and ended processing with L, or the server does not read .htaccess at all because AllowOverride is not set. Confirm the file is honoured before debugging the pattern.
- Does .htaccess work on nginx?
- No. It is an Apache feature. nginx redirects live in the server configuration and use entirely different syntax.
- Redirect or RewriteRule — which should I use?
- Redirect for a straightforward path-to-path move, RewriteRule when the redirect depends on a condition such as the host or the scheme.
- Do .htaccess files slow the site down?
- Slightly, because Apache checks for them on every request in every directory along the path. On a site you control at server level, put the rules in the main configuration instead.