Htaccess Redirect Generator
Need to set up a www-to-non-www or non-www-to-www redirect for your domain? The free Htaccess Redirect Generator by Amaze SEO Tools creates the exact Apache .htaccess rewrite rules you need — producing ready-to-paste server code that permanently redirects all traffic to your preferred domain format with a 301 status code.Amaze SEO Tools provides a free Htaccess Redirect Generator that produces Apache mod_rewrite rules for redirecting between www and non-www versions of your domain, ensuring all visitors and search engine crawlers reach a single, consistent URL format.
Every website is technically accessible at two addresses: www.example.com and example.com (without the www prefix). If both versions are live and serving the same content, search engines treat them as two separate websites with duplicate content — splitting your link equity, diluting your rankings, and creating indexing confusion. The solution is a permanent 301 redirect from one version to the other, consolidating all traffic and SEO signals under a single canonical domain format.
The redirect is implemented through Apache's .htaccess file using mod_rewrite rules. Writing these rules manually requires knowledge of Apache's rewrite syntax, regular expressions, and server condition flags — details that are easy to get wrong. Our generator eliminates the guesswork. Enter your domain, select the redirect direction, click Generate, and receive syntactically correct rewrite rules ready to paste into your .htaccess file.
Input Fields
Enter a Domain Name
A single-line input field is labelled "Enter a domain name" with the placeholder "example.com". Type your domain name without any protocol prefix — for example, yoursite.com or mybusiness.co.uk. Do not include http://, https://, or www. — just the bare domain. A clipboard icon on the right side of the field provides quick paste or clear functionality.
Select Redirect Type
Two radio buttons labelled "Select redirect type:" let you choose the direction of the redirect:
- Redirect from www to non-www — Visitors who access
www.yoursite.comare automatically redirected toyoursite.com. Choose this if you prefer the shorter, cleaner non-www format as your canonical domain. - Redirect from non-www to www (selected by default) — Visitors who access
yoursite.comare automatically redirected towww.yoursite.com. Choose this if you prefer the traditional www-prefixed format as your canonical domain.
reCAPTCHA (I'm not a robot)
Below the radio buttons, tick the "I'm not a robot" checkbox to pass the security verification before generating.
Action Button
Generate (Blue Button)
After entering your domain, selecting the redirect direction, and completing the reCAPTCHA, click "Generate" to produce the .htaccess rewrite code. The output appears in a code block below the form with a green copy button for easy clipboard copying.
How to Use Htaccess Redirect Generator – Step by Step
- Open the Htaccess Redirect Generator on the Amaze SEO Tools website.
- Enter your domain name — just the domain, without http://, https://, or www.
- Select the redirect direction — choose whether to redirect www → non-www or non-www → www.
- Tick the reCAPTCHA checkbox to verify yourself.
- Click "Generate" to produce the rewrite rules.
- Copy the generated code using the green copy button.
- Paste the code into your website's
.htaccessfile (located in your site's root directory). - Test the redirect by visiting both versions of your domain in a browser to confirm the redirect works correctly.
What the Generated Code Looks Like
Non-www to www Redirect (Default)
When you select "Redirect from non-www to www" for the domain example.com, the generator produces:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
This code tells the Apache server: if the incoming request is for example.com (without www), permanently redirect it to www.example.com — preserving the original path and query string.
www to Non-www Redirect
When you select "Redirect from www to non-www" for the domain example.com, the generator produces:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
This code tells the Apache server: if the incoming request is for www.example.com, permanently redirect it to example.com — preserving the original path and query string.
Understanding Each Line of the Code
RewriteEngine On
Activates Apache's mod_rewrite module for this directory. This line must be present before any rewrite conditions or rules can take effect. If mod_rewrite is not enabled on your server, these rules will not function and may cause a 500 Internal Server Error — contact your hosting provider to confirm mod_rewrite is available.
RewriteCond %{HTTP_HOST}
Defines the condition that must be met for the redirect to trigger. %{HTTP_HOST} is an Apache server variable containing the domain name from the incoming request. The condition pattern matches the domain format you want to redirect from. The [NC] flag makes the match case-insensitive, so it works regardless of whether the visitor types uppercase or lowercase letters in the URL.
RewriteRule ^(.*)$
Defines what happens when the condition is met. The pattern ^(.*)$ captures the entire URL path after the domain (the $1 backreference in the target URL preserves this path). The target URL is the preferred domain format with the original path appended. The flags [L,R=301] mean:
- L (Last) — Stop processing further rewrite rules after this one matches.
- R=301 — Send a 301 Moved Permanently HTTP status code, telling browsers and search engines that this redirect is permanent.
Why www vs Non-www Matters for SEO
Search engines treat www.example.com and example.com as technically distinct domains. If both versions serve the same content without a redirect, the consequences include:
- Duplicate content. Google sees two identical pages at two different URLs, which can confuse its indexing algorithms and dilute your rankings.
- Split link equity. External links pointing to your site may use either format. Without consolidation, the SEO value of those links is divided between two URLs instead of concentrating on one — weakening your overall domain authority.
- Inconsistent indexing. Google may index some pages under www and others without it, creating a fragmented presence in search results where neither version has the full strength of your complete site.
- Analytics fragmentation. Traffic data splits between two domain variations in analytics tools, making it harder to accurately measure performance and understand visitor behaviour.
A 301 redirect resolves all of these issues by funnelling all traffic, link equity, and indexing signals to a single canonical domain format.
www vs Non-www — Which Should You Choose?
There is no inherent SEO advantage to either format. Google treats both equally. The choice comes down to preference and technical considerations:
- Non-www (example.com) — Shorter, cleaner, and increasingly popular among modern websites. Saves characters in printed materials and verbal communication. Most newer brands and startups default to this format.
- www (www.example.com) — The traditional format that is instantly recognisable as a website address. Offers a minor technical advantage: www is a subdomain that can have its own DNS CNAME record, making it slightly easier to point to CDN services or failover configurations. Some enterprise and legacy systems expect the www prefix.
The most important thing is consistency — pick one and redirect the other. If your site already has significant backlinks and indexed pages under one format, keep that format as the canonical to preserve existing SEO equity.
Real-World Use Cases
1. Consolidating Domain Versions for SEO
The primary use case. Every website should redirect one domain format to the other to prevent duplicate content, consolidate link equity, and present a single canonical URL to search engines. This is a foundational technical SEO requirement.
2. Setting Up a New Website Correctly From Day One
When launching a new website, configuring the www/non-www redirect before any content is published or any links are built ensures you start with a clean, consolidated domain structure from the very beginning — avoiding SEO issues that become harder to fix later.
3. Fixing Duplicate Content Warnings in SEO Audits
If an SEO audit tool (Google Search Console, Screaming Frog, Ahrefs) flags duplicate content between www and non-www versions of your pages, this generator produces the exact code needed to resolve the issue.
4. Supporting HTTPS Migration
When migrating from HTTP to HTTPS, the generated code also serves as a foundation for combining the www/non-www redirect with an HTTP-to-HTTPS redirect — ensuring all four possible URL variations (http://example.com, http://www.example.com, https://example.com, https://www.example.com) consolidate to a single preferred format.
5. Configuring Redirects After a Domain Change
When rebranding or changing domains, the www/non-www redirect on the new domain should be configured immediately to ensure the new domain starts with proper URL consolidation.
6. Resolving Google Search Console Discrepancies
Google Search Console tracks www and non-www properties separately. If you see data split between two properties, a redirect consolidates all traffic under one property — giving you a complete, unified view of your site's search performance.
How to Add the Code to Your .htaccess File
- Locate your .htaccess file. It is in the root directory of your website (the same directory as your homepage's
index.htmlorindex.php). If you cannot see it, enable "show hidden files" in your FTP client or file manager — files starting with a dot are hidden by default. - Back up the existing file. Before making any changes, download a copy of your current
.htaccessfile. A syntax error in this file can take your entire site offline, so always keep a backup. - Open the file for editing. Use your hosting control panel's file manager, an FTP client like FileZilla, or an SSH terminal.
- Paste the generated code. Add the three lines at the top of the file, before any existing rewrite rules. If the file already contains
RewriteEngine On, do not duplicate it — just add the RewriteCond and RewriteRule lines below the existingRewriteEngine Ondirective. - Save and test. Save the file, then visit both versions of your domain in a browser (with and without www) to confirm the redirect works. Use the HTTP Status Code Checker by Amaze SEO Tools to verify the redirect returns a 301 status code.
Important Notes and Troubleshooting
- This code works on Apache web servers only. The
.htaccessfile and mod_rewrite are Apache-specific. If your site runs on Nginx, LiteSpeed, or IIS, you will need equivalent configuration in those servers' respective formats. Many shared hosting providers run Apache, but verify with your host if unsure. - mod_rewrite must be enabled. The rewrite rules require Apache's mod_rewrite module. Most hosting providers enable it by default, but if you receive a 500 error after adding the code, contact your host to confirm the module is active.
- Order matters in .htaccess. If your file contains multiple rewrite rules, the order in which they appear affects processing. The www/non-www redirect should generally appear early in the file — before WordPress rewrite rules, custom routing, or other application-specific directives.
- Combine with HTTPS redirect if needed. If your site uses HTTPS (which it should), you may want to combine the www/non-www redirect with an HTTP-to-HTTPS redirect in a single rule set to avoid redirect chains.
- Clear browser cache after testing. Browsers cache 301 redirects aggressively. If the redirect does not seem to work after changes, clear your browser cache or test in an incognito/private window.
- Set the matching canonical in Google Search Console. After implementing the redirect, set your preferred domain format in Google Search Console to reinforce the consolidation signal.
Frequently Asked Questions
Q: What redirect type does the generator use?
A: The code uses a 301 Moved Permanently redirect, which is the recommended type for permanent URL consolidation. A 301 tells search engines to transfer all ranking signals from the old URL to the new one and to update their index accordingly.
Q: Does it matter whether I choose www or non-www?
A: No — there is no SEO ranking difference between the two formats. Choose whichever you prefer, and redirect the other. If your site already has established backlinks and indexed pages under one format, keep that format to preserve existing SEO equity.
Q: Will the redirect preserve the full URL path?
A: Yes. The generated code uses the $1 backreference to capture and preserve the entire URL path and query string. A visitor accessing example.com/blog/my-post?ref=twitter will be redirected to www.example.com/blog/my-post?ref=twitter (or vice versa) — the path and parameters are fully preserved.
Q: What if my site is on Nginx instead of Apache?
A: This tool generates Apache .htaccess rules that will not work on Nginx. For Nginx, you need equivalent server block configuration with a return 301 directive. Consult your Nginx documentation or hosting provider for the correct syntax.
Q: Can this cause a redirect loop?
A: Not if configured correctly. The RewriteCond specifically matches only the domain version you want to redirect from, so requests to the preferred version pass through without triggering the rule. However, conflicting rules elsewhere in your .htaccess file or in your CMS settings could create loops — always test after implementation.
Q: Should I also set a canonical tag?
A: Yes. While the 301 redirect handles the server-level consolidation, adding a <link rel="canonical"> tag pointing to the preferred URL on each page provides a reinforcing signal to search engines. Use the Meta Tag Generator to include canonical references in your page metadata.
Q: What happens if I already have RewriteEngine On in my .htaccess?
A: Do not duplicate the RewriteEngine On line. If it already exists in your file, simply add the RewriteCond and RewriteRule lines below it. Having multiple RewriteEngine On declarations is unnecessary but typically does not cause errors.
Q: Is my domain stored or shared?
A: No. The domain name you enter and the generated code are not saved, logged, or transmitted to any third-party service. The generation runs entirely within the tool interface.
Generate Apache .htaccess redirect rules for www and non-www domain consolidation — use the free Htaccess Redirect Generator by Amaze SEO Tools to create 301 redirect code that unifies your domain, eliminates duplicate content, consolidates link equity, and strengthens your site's technical SEO foundation!