Free .htaccess Redirect Generator
Generate redirect rules to send WWW to non-WWW or non-WWW to WWW for Apache-based websites.
A website can sometimes be accessible through both www.example.com and example.com. Although both addresses may show the same website, it is usually better to choose one preferred hostname and redirect visitors from the other version.
ChanduSEOTool's .htaccess Redirect Generator helps create redirect rules for two common hostname setups: redirecting WWW to non-WWW or redirecting non-WWW to WWW. Enter your domain, select the redirect direction you want, and generate the rule for review.
This guide explains why hostname redirects matter, how .htaccess redirects work, the difference between WWW and non-WWW, how permanent and temporary redirects differ, what to check before editing an .htaccess file, and how redirects relate to canonical URLs and search indexing.
Table of Contents
- What Is an .htaccess Redirect?
- How to Use the .htaccess Redirect Generator
- WWW vs Non-WWW
- Why Redirect One Hostname Version?
- 301 vs 302 Redirects
- Example Redirect Rules
- Redirects, Canonicals and SEO
- Where to Add .htaccess Rules
- How to Test a Redirect
- Common Redirect Mistakes
- Frequently Asked Questions
What Is an .htaccess Redirect?
An .htaccess file is a configuration file commonly used with Apache HTTP Server. Depending on the server configuration, it can control behavior such as URL rewriting, redirects, access rules and other directory-level settings.
A redirect tells a browser or crawler that a requested URL should be accessed at another URL instead. For example, a website owner may want every request for:
https://www.example.com/page
to reach:
https://example.com/page
The opposite setup is also possible. A site can use the WWW version as its preferred hostname and redirect non-WWW requests to it.
The important point is consistency. You normally want visitors, internal links, canonical tags, sitemap URLs and redirects to agree on the version of the domain that you consider primary.
How to Use the .htaccess Redirect Generator
The ChanduSEOTool generator is designed around a simple hostname redirect workflow.
- Enter your domain name in the domain field.
- Choose whether you want to redirect WWW to non-WWW or non-WWW to WWW.
- Generate the redirect rule.
- Review the generated configuration before adding it to your website.
- Back up your existing
.htaccessfile. - Add the rule in the appropriate location.
- Test several URLs after the change.
Do not overwrite an existing .htaccess file without first checking what it already contains. WordPress, Laravel-related hosting configurations, security plugins, caching systems and other applications may already use rewrite rules.
WWW vs Non-WWW: What Is the Difference?
Consider these two addresses:
https://www.example.com
and:
https://example.com
The first uses the www hostname and the second uses the root or non-WWW hostname.
For many ordinary websites, neither choice is automatically better for SEO. What matters more is choosing the version that works for your technical setup and using it consistently.
A business may choose WWW because its infrastructure is already configured around that hostname. Another site may choose non-WWW because it produces a shorter address. Both approaches can work when redirects, HTTPS, canonical tags and internal links are configured correctly.
Why Redirect WWW to Non-WWW or Non-WWW to WWW?
Keep One Consistent Website Address
If both hostname versions load the same content without a redirect, users and crawlers may encounter more than one URL for essentially the same page.
A hostname redirect makes the preferred address clearer and creates a more consistent browsing experience.
Consolidate URL Signals
Links may sometimes point to both the WWW and non-WWW versions of a site. Redirecting one hostname to the preferred hostname helps establish a consistent destination.
Make Internal Linking Cleaner
Internal links should ideally point directly to the preferred URL rather than relying on a redirect every time a visitor clicks a link.
For example, if your preferred domain is https://example.com, your menus, articles, sitemap and other internal links should generally use that hostname directly.
Reduce Duplicate URL Variants
Search engines can encounter multiple URL variants through hostname differences, HTTP and HTTPS versions, tracking parameters or other technical variations.
Redirects are one of several signals that can help indicate which URL should represent the content.
301 vs 302 Redirect: What Is the Difference?
A redirect does more than send a browser to a different address. The HTTP status code also tells clients something about the nature of the move.
301 Permanent Redirect
A 301 redirect indicates that a resource has moved permanently. It is commonly appropriate when you have permanently decided that one hostname should redirect to another hostname.
For example, if your final site structure permanently uses non-WWW URLs, a permanent redirect from WWW to non-WWW can make sense.
302 Temporary Redirect
A 302 redirect indicates a temporary redirect. It may be useful when the change is not intended to be permanent.
With Apache mod_rewrite, using the redirect flag without explicitly specifying a redirect status can result in a temporary 302 response. Therefore, always inspect the generated rule and confirm that its status code matches your intended use.
Example WWW and Non-WWW Redirect Rules
The following examples are educational examples for a fictional domain. Your server configuration may require different rules.
Example: WWW to Non-WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
This example checks for the WWW hostname and sends requests to the non-WWW HTTPS version using a permanent redirect.
Example: Non-WWW to WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
This example does the reverse and sends the root hostname to the WWW hostname.
Do not simply copy an example rule into a production site without adapting the domain and checking how HTTPS, proxies, CDN services, virtual hosts and existing rewrite rules are configured.
Does the Redirect Preserve Page Paths?
A well-configured hostname redirect should normally preserve the requested path.
For example:
https://www.example.com/blog/article
should ideally redirect to:
https://example.com/blog/article
rather than redirecting every request to only the homepage.
Preserving the path is especially important on established websites because visitors and search engines may access deep URLs directly.
What About Query Parameters?
URLs may include query strings such as:
https://www.example.com/product?id=25
Redirect behavior for query parameters depends on how the rule is written. Apache rewrite rules normally preserve existing query strings unless the configuration explicitly changes or discards them.
Always test query-string URLs if your website relies on parameters for search, filtering, tracking or application behavior.
WWW Redirects, Canonical URLs and SEO
Redirects and canonical tags are related but they are not identical.
A redirect actively sends a visitor or crawler from one URL to another. A canonical tag is an HTML signal that indicates which URL you prefer to be treated as the representative version of substantially duplicate content.
For a normal hostname migration or permanent hostname preference, a server-side redirect is a strong technical signal. Your canonical tags should normally agree with that setup.
If your preferred page is:
https://example.com/page
then it is generally cleaner for that page to use a self-referencing canonical such as:
<link rel="canonical" href="https://example.com/page">
Your sitemap and internal links should also use the preferred version where practical.
Google may consider redirects, canonical annotations, HTTPS and sitemap inclusion among the signals it uses when selecting a canonical URL.
Redirects and Duplicate Domains
Hostname duplication becomes especially important if the same website is accidentally accessible through another hostname, staging domain, server alias or old domain.
If duplicate hosts serve identical pages, crawlers can potentially discover multiple copies of the same content. A permanent redirect from an obsolete hostname to the preferred domain is often more appropriate than leaving both hosts serving duplicate pages.
When changing domains or removing an old hostname, do not remove the old DNS configuration immediately if crawlers still need to reach the old hostname and receive its redirect.
Keep the redirect available long enough for users, search engines and external links to transition to the preferred destination.
Redirect Loops: What Are They?
A redirect loop occurs when URLs repeatedly redirect to each other.
For example:
WWW redirects to non-WWW, while another rule redirects non-WWW back to WWW.
The browser cannot reach a final page because it keeps moving between the two addresses.
Before adding a new hostname rule, inspect existing redirect rules in your hosting panel, CDN, web server, application and .htaccess file.
Redirect Chains
A redirect chain occurs when one URL redirects through several intermediate URLs before reaching the final destination.
For example:
HTTP WWW → HTTPS WWW → HTTPS non-WWW
A cleaner setup may be able to send the initial request directly to the final preferred URL.
Reducing unnecessary redirect steps can simplify your configuration and reduce extra requests.
Where Should You Add the .htaccess Redirect Rule?
The correct location depends on your hosting environment.
For many Apache-hosted websites, the relevant .htaccess file is located in the site's document root, often the same directory containing the main public website files.
Before changing it:
- Download or copy the existing file as a backup.
- Check whether
RewriteEngine Onis already present. - Review application-generated rewrite rules.
- Avoid inserting duplicate or conflicting hostname conditions.
- Save the change and immediately test the website.
A syntax or configuration error in .htaccess can cause server errors or make the website inaccessible, so changes should be made carefully.
Does Every Web Server Use .htaccess?
No.
The .htaccess mechanism is associated with Apache-compatible server configurations. Nginx normally uses server configuration files rather than .htaccess files.
Some managed hosts, reverse proxies and CDN platforms may handle redirects outside Apache completely.
If your hosting environment does not support .htaccess overrides, generated .htaccess rules will not be the correct configuration method.
Apache mod_rewrite and .htaccess
Apache's mod_rewrite module provides flexible URL rewriting and redirection capabilities.
Rewrite behavior in an .htaccess file is evaluated in a per-directory context, so rules can behave differently from equivalent rules placed directly in the main server or virtual-host configuration.
If you have full server access, Apache documentation notes that simpler server-level redirect methods may sometimes be preferable to using mod_rewrite. The .htaccess approach is particularly useful when server configuration access is limited.
How to Test a WWW or Non-WWW Redirect
Testing is essential after making redirect changes.
Check at least:
- The homepage.
- A normal internal page.
- A deep article or tool URL.
- An HTTP URL if HTTP is still reachable.
- The WWW hostname.
- The non-WWW hostname.
- A URL containing query parameters when your site uses them.
The final browser address should be your preferred hostname, and the original page path should normally remain intact.
You can also use the Redirect Checker to inspect redirect behavior and status information.
Check Your Canonical After Redirect Changes
After setting a hostname redirect, inspect the HTML source of important pages.
The canonical URL should normally point to the preferred live hostname rather than the version that redirects away.
If Google Search Console previously selected another canonical, remember that its indexed report may reflect an older crawl. Google needs to recrawl the relevant URLs before its reporting can reflect new redirect and canonical signals.
Update Internal Links
A redirect should not be used as a substitute for clean internal linking.
Once you have selected your preferred hostname, update navigation links, content links and other important internal references to point directly to that version whenever possible.
This avoids unnecessary redirect hops and gives users and crawlers a consistent site structure.
You can use the Google Index Checker when reviewing whether important URLs are discoverable in Google, although Search Console remains the better source for detailed ownership-based indexing diagnostics.
Update Your Sitemap
Your XML sitemap should normally contain the canonical URLs you want search engines to crawl and index.
If your preferred hostname is non-WWW, avoid intentionally publishing WWW versions of the same pages in the sitemap unless your site architecture specifically requires them.
The same principle applies in reverse if WWW is your chosen canonical hostname.
Common .htaccess Redirect Mistakes
Redirecting Both Directions
Do not configure WWW to non-WWW and non-WWW to WWW at the same time. That can create a redirect loop.
Using the Wrong Domain
Always review generated code before installation. A typo in the source or destination hostname can send visitors to the wrong address.
Forgetting HTTPS
If your site uses HTTPS, make sure the final redirect destination uses the correct secure URL.
Hostname redirection and HTTP-to-HTTPS redirection should work together without creating multiple unnecessary hops.
Overwriting Existing .htaccess Rules
Never replace a working .htaccess file blindly. Existing application, CMS, caching or security rules may be essential to the website.
Not Testing Inner Pages
Testing only the homepage is not enough. Confirm that deep page URLs preserve their paths after redirection.
Creating Redirect Chains
Whenever practical, send requests directly to the final preferred URL rather than through multiple intermediate redirects.
Assuming Every Server Supports .htaccess
An .htaccess rule is not universal web-server configuration. Confirm that your hosting environment supports it.
Safety Before Editing .htaccess
An incorrect .htaccess rule can cause errors across an entire website.
Before making changes:
- Create a backup.
- Keep access to your hosting file manager or FTP account.
- Know how to restore the previous version.
- Make one configuration change at a time.
- Test the website immediately after saving.
If a change causes a server error, restore the previous .htaccess file before making further changes.
Related ChanduSEOTool Website Tools
These related tools can help when checking redirects and other technical website details:
- Redirect Checker – inspect a URL's redirect behavior.
- Hosting Checker – review hosting-related information for a domain.
- Meta Tags Analyzer – inspect important meta information on a webpage.
- Google Index Checker – check search-index visibility as part of a broader SEO review.
Frequently Asked Questions
What is an .htaccess redirect?
It is a redirect configured through an Apache-compatible .htaccess file. It can send requests from one URL or hostname to another when the server permits .htaccess-based rewriting or redirects.
Can I redirect WWW to non-WWW?
Yes. The generator provides an option for creating a rule intended to redirect the WWW hostname to the non-WWW hostname.
Can I redirect non-WWW to WWW?
Yes. Select the opposite redirect direction when WWW is the hostname you want visitors to use.
Which is better for SEO, WWW or non-WWW?
There is no universal SEO advantage simply from choosing WWW or non-WWW. A technically consistent preferred hostname, correct redirects, canonicals and internal links are more important.
What is a 301 redirect?
A 301 HTTP status indicates a permanent redirect. It is commonly used when a URL or hostname has permanently moved to another location.
What is a 302 redirect?
A 302 response is a temporary redirect. It communicates that the redirect is not necessarily intended to be permanent.
Does Apache use 301 automatically?
Not necessarily. With mod_rewrite, the redirect flag can default to a 302 response when no specific redirect status is supplied. Check the generated rule and specify a permanent status when that is your intended configuration.
Can a bad .htaccess rule break my website?
Yes. Invalid syntax or conflicting rules can cause server errors, redirect loops or unexpected URL behavior. Always create a backup before changing the file.
Does Nginx use .htaccess?
Normally no. Nginx uses its own server configuration system, so Apache .htaccess rules cannot simply be copied into an Nginx configuration.
Should my canonical URL match the redirect destination?
For a normal hostname canonicalization setup, it is generally cleaner for canonical tags, internal links, sitemap URLs and redirects to consistently point toward the preferred hostname.
Should I remove my old hostname immediately?
If an old hostname is redirecting to a new or preferred domain, keeping that redirect reachable can be important for users, crawlers and old external links. Do not remove the old host before the transition has had sufficient time to be processed.
How can I verify my redirect?
Open the old hostname in a private browser window, check the final URL, test deep pages and use a redirect-checking tool to inspect the HTTP redirect behavior.
Generate a WWW or Non-WWW .htaccess Redirect
Use ChanduSEOTool's .htaccess Redirect Generator when you need a simple starting point for a WWW-to-non-WWW or non-WWW-to-WWW hostname redirect.
Enter the domain, choose the direction, generate the rule, and review it carefully before changing a live website. Keep your preferred hostname consistent across redirects, canonical tags, sitemap URLs and internal links.
After deployment, test the homepage as well as deep URLs to confirm that visitors reach the correct hostname without redirect loops or unnecessary chains.