.htaccess Redirect Generator

Generate Apache .htaccess redirect rules. Add your redirects below and copy the code.

Redirect Type
Generated .htaccess Code

What Is a 301 Redirect?

A 301 redirect is a permanent HTTP redirect that tells browsers and search engines that a URL has moved to a new location and the move is final. When search engines encounter a 301, they transfer most of the link equity (PageRank, authority, backlink value) from the old URL to the new one. Over time, the old URL is removed from search results and replaced by the new URL. A 302 redirect, by contrast, is temporary — the old URL stays in search results because Google assumes the content will return.

For any permanent URL change — renaming a page, restructuring categories, migrating to HTTPS, consolidating content — always use 301. The only times 302 is appropriate are genuinely temporary redirects like A/B test variants or maintenance pages.

When to Use Each Rule Type

Single-Page Redirects

Use when specific URLs have moved. Perfect for renamed articles, deleted product pages redirected to category, or consolidating multiple old URLs into one new authoritative page.

Redirect 301 /old-article https://www.example.com/new-article
Redirect 301 /products/old-name https://www.example.com/products/new-name

Domain Redirect

Use when moving the entire site to a new domain. Redirects every path on the old domain to the same path on the new domain, preserving deep-link structure.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [R=301,L]

HTTP to HTTPS

Essential for every modern site. Forces every HTTP request to use HTTPS, ensuring no one lands on an insecure version.

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

WWW vs Non-WWW Canonicalisation

Pick one version as canonical and redirect the other. This prevents duplicate-content issues and consolidates link equity to a single URL.

<!-- Force WWW -->
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

<!-- Force non-WWW -->
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

How .htaccess Works

.htaccess is a per-directory configuration file used by Apache web servers. When placed in your site's root directory, it controls how Apache handles requests to your site — redirects, rewriting URLs, access control, caching headers, and more. The file is read on every request, so complex rules can have a small performance cost (typically negligible for sites under heavy traffic).

The RewriteEngine On line at the start enables the mod_rewrite module, which is what powers all the complex redirect logic. Most Apache installations have it enabled by default. If your rules don't work, confirm mod_rewrite is enabled.

Common .htaccess Redirect Mistakes

Missing RewriteEngine On

Every file containing RewriteCond or RewriteRule must start with RewriteEngine On. Without it, the rules are ignored silently.

Wrong Escape Characters

Periods in hostnames need escaping: ^olddomain\.com$ not ^olddomain.com$. Unescaped dots are regex wildcards and match any character.

Redirect Chains

Chaining redirects (A → B → C) wastes crawl budget and slightly dilutes link equity. Always redirect directly to the final destination.

Using Redirect Instead of RewriteRule

The simple Redirect directive doesn't support pattern matching. For anything beyond single-URL redirects, use RewriteRule.

Missing [R=301,L] Flags

The R=301 flag specifies the redirect is permanent. The L flag tells Apache to stop processing further rules after this one. Without both, redirects may be treated as 302 or cause rule conflicts.

After Generating: Deploy and Test

  1. Copy the generated code.
  2. Locate your .htaccess file. In the root of your web directory (usually public_html or www).
  3. Back up the existing file before making changes.
  4. Add the new rules. Place near the top, after any RewriteBase directive.
  5. Test each redirect. Visit old URLs to confirm they redirect correctly. Use the redirect checker to verify the redirect chain and status codes.
  6. Submit the updated sitemap. After site-wide redirects (like HTTPS migration), re-submit your sitemap in Google Search Console.
  7. Monitor Search Console. Watch for 404 errors or redirect warnings in the Coverage report.

Not on Apache?

ServerConfig FileSyntax
Apache.htaccessRewriteRule / Redirect (this generator)
Nginxnginx.confreturn 301 / rewrite
IISweb.config<rewrite> XML rules
CloudflareDashboard rulesRedirect Rules at the edge
Next.js / Vercelnext.config.jsredirects() function

Related Redirect Tools