When should I use .htaccess for redirects instead of application-level redirects?
Use .htaccess (or nginx config) for permanent infrastructure redirects: HTTPS enforcement, www to non-www, old domain to new domain, and removing trailing slashes. These should run at the web server level for performance and consistency. Use application-level redirects for content-specific moves like blog post URL changes, where business logic needs to determine the redirect target. Mixing the two layers is fine, but be careful to avoid loops where both layers redirect the same URL.
What is the difference between Redirect, RedirectMatch, and RewriteRule?
Redirect handles simple URL-to-URL mappings using prefix matching. RedirectMatch supports regex pattern matching for groups of URLs. RewriteRule is the most powerful, supporting complex regex, conditions (RewriteCond), and internal rewrites that change the URL without redirecting. For most sites, Redirect 301 handles the simple cases, and RewriteRule with [R=301,L] flags handles regex-based bulk migrations. RewriteRule is part of mod_rewrite and requires it enabled on the server.
Why do my .htaccess redirects work in some places but fail in others?
Common reasons: AllowOverride is not set to All in the main Apache config, mod_rewrite is not enabled, .htaccess is in the wrong directory (must be in the document root or matching subdirectory), or another .htaccess file higher in the tree is overriding yours. Also, rule order matters: more specific rules must come before general ones, or the general rule catches first. Test changes in a staging environment because a broken .htaccess can take the entire site offline.
How do I avoid redirect loops in .htaccess?
Always include conditions that exclude the destination URL. For HTTPS redirects, use RewriteCond %{HTTPS} off before the rule. For www to non-www, use RewriteCond %{HTTP_HOST} ^www\. as a pre-check. Without conditions, the redirect fires recursively and the browser errors out. Test with curl -I -L to follow the chain and confirm it terminates in 1-2 hops. Most loops happen when administrators add rules without checking existing rules in the same file.
What is the performance impact of many .htaccess rules?
Apache reads .htaccess on every request for every directory in the URL path, so dozens of rules add measurable latency, often 5-20ms per request. For large rule sets, move directives to the main httpd.conf file (read once at startup) and disable .htaccess processing. On nginx, all rules live in the main config by default, which is faster. If your site has hundreds of redirect rules, consider a database-backed redirect plugin with caching instead of .htaccess.
Should I use 301 or 302 in my .htaccess redirects?
Use 301 for permanent moves (the default for migrations and HTTPS upgrades) so search engines transfer ranking signals to the new URL. Use 302 only for genuinely temporary redirects like maintenance pages or A/B tests. The most common mistake is using 302 by accident: some hosting panels and plugins default to 302, silently breaking SEO during migrations. Always specify [R=301,L] explicitly in RewriteRule and audit existing rules after any panel-driven changes.