.htaccess Redirect Generator
Generate Apache .htaccess redirect rules. Add your redirects below and copy the 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
- Copy the generated code.
- Locate your .htaccess file. In the root of your web directory (usually
public_htmlorwww). - Back up the existing file before making changes.
- Add the new rules. Place near the top, after any
RewriteBasedirective. - Test each redirect. Visit old URLs to confirm they redirect correctly. Use the redirect checker to verify the redirect chain and status codes.
- Submit the updated sitemap. After site-wide redirects (like HTTPS migration), re-submit your sitemap in Google Search Console.
- Monitor Search Console. Watch for 404 errors or redirect warnings in the Coverage report.
Not on Apache?
| Server | Config File | Syntax |
|---|---|---|
| Apache | .htaccess | RewriteRule / Redirect (this generator) |
| Nginx | nginx.conf | return 301 / rewrite |
| IIS | web.config | <rewrite> XML rules |
| Cloudflare | Dashboard rules | Redirect Rules at the edge |
| Next.js / Vercel | next.config.js | redirects() function |
Related Redirect Tools
- Redirect checker — verify redirects work.
- What is a 301 redirect? — reference guide.
- How to fix broken links — full playbook.
- Broken link checker — find dead links.
- Meta refresh checker — find client-side redirects to replace.