How .htaccess Redirects Work
Apache's .htaccess file is the most common way to manage URL redirects on shared hosting and many dedicated server setups. When a browser requests a URL, Apache checks for a .htaccess file in the requested directory and applies any matching rules before serving the response. Redirect rules tell Apache to send the browser to a different URL, optionally passing along a status code that indicates whether the move is permanent or temporary.
There are two primary syntaxes for redirects: the simpler Redirect directive (ideal for one-to-one URL mappings) and the more powerful RewriteRule directive (which supports regular expressions and conditional logic via RewriteCond). This generator uses Redirect for simple paths and RewriteRule when you enable regex or domain-level rules.
When to Use Each Redirect Type
Choosing the right HTTP status code matters — it affects how search engines treat the redirect and how browsers cache it. A 301 Permanent Redirect is the workhorse for SEO. It tells Google to transfer all ranking signals from the old URL to the new one and to deindex the original. Use it for site migrations, URL restructuring, or consolidating duplicate pages.
A 302 Temporary Redirect preserves the original URL's rankings in search engines. Use it when you're running A/B tests, performing maintenance, or serving localized content that may change. The 307 code is identical to 302 but strictly preserves the HTTP method (GET, POST) — it's important for API endpoints or form submissions where method fidelity matters.
Domain-Level Best Practices
Before adding individual redirects, establish your canonical domain configuration. Every site should enforce HTTPS and choose between www and non-www. Running both versions creates duplicate content that splits your search rankings. Use our domain-level toggles to generate the correctRewriteCond + RewriteRule blocks for these foundational rules — they should appear first in your .htaccess file so they execute before any path-specific redirects.
Trailing slash consistency is equally important. Pick a convention (with or without trailing slash) and enforce it via redirect. This prevents duplicate content issues where /about and/about/ are treated as separate pages by search engines.