Htaccess Redirect
Generate .htaccess redirect code for www to non-www or non-www to www, so your site uses one canonical domain and avoids duplicate URL versions.
About Htaccess Redirect
Htaccess Redirect Generator: WWW to Non-WWW (and Back)
If your site loads on both www and non-www versions, you’re basically running two addresses for the same content. This htaccess redirect generator helps you generate the correct .htaccess code to redirect www → non-www or non-www → www with a proper 301.
This isn’t a “nice to have.” It’s one of those boring technical fixes that quietly prevents SEO weirdness and user confusion. Without a canonical redirect, Google can index both versions, analytics can split traffic, cookies can behave differently, and your backlinks may point to mixed domains. And the worst part? You only notice after months when something feels off. A clean htaccess redirect is how you stop that at the source.
How Htaccess Redirect Works
The tool is built around one decision and one input: you enter your domain, choose the redirect direction, and generate the code. No extra toggles to misconfigure. That simplicity is intentional because most redirect problems come from “one more rule” layered on top of unknown existing rules.
- Step 1: Enter your domain in the Domain Name field (the placeholder is “Enter a domain”). Use the root domain format you actually own, like example.com.
- Step 2: Under Select Redirect Type, choose one option: From WWW to Non-WWW or From Non-WWW to WWW.
- Step 3: Click Get Your HtAccess Code. The tool generates your redirect code automatically.
- Step 4: In the results section, you’ll see a short note telling you to copy the .htaccess code. The code appears in a textarea.
- Step 5: Click Copy to Clipboard or Save as TXT. Then paste the rules into your site’s .htaccess file (typically in the web root for Apache).
One important detail: this tool is specifically focused on canonicalizing the domain version (www vs non-www). It’s not trying to generate every redirect under the sun. That makes the output easier to trust and easier to troubleshoot.
Key Features
Two-direction redirect selection (no guessing)
Most people don’t need a dozen redirect “templates.” They need one: the canonical choice between www and non-www. The tool gives you a clear radio selection: From WWW to Non-WWW or From Non-WWW to WWW.
That’s helpful because the “correct” answer depends on your setup. Some brands prefer the shorter non-www. Others need www for legacy reasons, CDN configs, or cookie scopes. Either way, you choose once and apply consistently.
Copy-ready .htaccess output you can save as a file
The result is presented as plain htaccess code in a textarea. No extra formatting, no hidden characters, no “download a zip.” You can copy it directly or use Save as TXT to keep a record.
This matters when you’re working across environments. You can generate one set of rules for staging, another for production, and keep them labeled. And if something goes wrong, you can roll back quickly because you saved what you changed.
Designed to reduce common redirect mistakes
Redirects fail in predictable ways: loops, mixed protocols, or rules that don’t match subdomains the way you think they do. A focused htaccess redirect generator reduces these errors by producing a consistent pattern for a very specific job: www/non-www canonicalization.
You still need to place the rules carefully in your .htaccess (especially if you have existing rewrite rules), but starting with clean, structured code is half the battle.
SEO-friendly intent: consolidate duplicate URLs
From an SEO perspective, the point of this redirect is consolidation. You want one canonical domain so search engines and users land on the same URL version every time.
When the site uses one preferred version, signals like backlinks, social shares, and crawl efficiency stop being diluted across two hosts. It’s not glamorous, but it’s foundational.
Use Cases
People reach for .htaccess redirects when they’re cleaning up technical debt, migrating sites, or fixing subtle SEO problems that come from inconsistent URLs.
- SEO specialist: Fix www/non-www duplication so indexing and canonical signals consolidate properly.
- Developer: Standardize domain behavior across environments and stop mixed-host bugs.
- Agency: Apply a consistent canonical pattern across multiple client sites hosted on Apache.
- Site owner: Clean up URL variants after noticing analytics traffic split across two hosts.
- E-commerce manager: Prevent duplicate product URLs when external links point to both versions.
- Content team: Ensure shared links resolve to a single version for brand consistency.
- Sysadmin: Standardize redirect rules before enabling additional security or caching layers.
- Migrations lead: Lock down the canonical host before moving to HTTPS or changing CMS.
Scenario example #1: You notice Google Search Console showing both https://example.com and https://www.example.com indexed, with similar pages duplicated. You pick one canonical (say non-www), generate the www→non-www htaccess redirect, deploy it, and within days the duplicates start consolidating.
Scenario example #2: Your marketing team shares links using www because that’s what they’ve always done, but your app uses non-www for cookies and session management. Users occasionally get logged out when moving between versions. You standardize the host with a 301 redirect and the session weirdness disappears.
Scenario example #3: After a redesign, you inherit an .htaccess file with old rewrite rules. The site “works,” but redirects are inconsistent. You use this generator to create a clean canonical rule, then place it at the top so host normalization happens before any other rewriting.
When to Use Htaccess Redirect vs. Alternatives
You can implement canonical host redirects in different layers: .htaccess, web server config, or CDN. Here’s when this tool is the right move, and when you might choose another approach.
| Scenario | Htaccess Redirect | Manual approach |
|---|---|---|
| You run Apache and can edit .htaccess | Generate copy-ready rules and paste them in minutes. | Write mod_rewrite rules by hand and risk typos or bad patterns. |
| You need www ↔ non-www canonicalization | Tool is purpose-built for this exact redirect. | Search snippets online; mix incompatible rules; create loops. |
| You want easy rollback | Save as TXT and keep a change record. | Edits are often undocumented and hard to revert cleanly. |
| You have access to server config or CDN rules | Still works, but may be redundant. | Better to set canonical host in one central layer. |
| You’re debugging a redirect loop | Generate a clean baseline rule to compare against. | Guess which existing rule is causing the loop; change blindly. |
If you can set redirects at the server config or CDN edge, that can be even cleaner. But when .htaccess is the layer you control—and you just need www/non-www canonicalization—this generator is the quickest safe path.
Tips for Getting the Best Results
Decide your canonical host first, then stick to it everywhere
Pick one: www or non-www. Then use it consistently in your CMS settings, analytics property, marketing links, and internal linking. The redirect enforces the decision, but consistency prevents churn and “mixed host” surprises.
Place the canonical redirect early in the .htaccess file
Host normalization should happen before other rewrite rules. If you redirect later, other rewrites might fire first, and you can end up with confusing chains. Keeping the canonical redirect near the top makes behavior easier to reason about.
Watch for redirect loops when other rules already exist
Loops usually happen when you have overlapping rules: one forces www, another removes www, or a plugin tries to do its own canonicalization. If you see a loop, temporarily disable competing rules and apply one canonical redirect at a time.
Remember that 301 is the “canonical” choice for permanent host redirects
For www/non-www standardization, you usually want a permanent redirect. That signals that the old host is not the primary version. Temporary redirects can keep both versions in play longer than you want.
Simple testing checklist
Test: http://www, http://non-www, https://www, https://non-www. All paths should end up on one canonical host with one clean redirect hop. If you see multiple hops, simplify your rules and re-test.
Frequently Asked Questions
An htaccess redirect is a rule placed in an Apache .htaccess file that tells browsers and bots to request a different URL. For www vs non-www, it’s commonly used to force a single canonical host so your site doesn’t behave like two separate sites.
You need it when both versions load the same content. Without a redirect, you risk duplicate indexing, split analytics, and inconsistent link sharing—especially when different people or tools copy different URL versions.
There’s no universal “best.” Choose the version that matches your existing branding, historic backlinks, and technical setup. Some teams prefer non-www because it’s shorter. Others prefer www because it’s explicit and sometimes easier in certain DNS/CDN setups.
The key is consistency. Pick one canonical host and enforce it with a 301 redirect. Once that’s done, update internal links and settings so you stop generating mixed URLs.
A 301 redirect indicates a permanent move. For canonicalization, it tells search engines that one host is the primary version and the other should be treated as a redirecting alias.
In practice, this helps consolidate signals like backlinks and crawling to the canonical host. It also reduces the chances of duplicate pages being indexed under two different domains.
Typically, you place canonical host redirects near the top of the .htaccess file in your site’s web root. That way, requests are normalized before other rewrite rules or CMS routing logic runs.
If you already have a complex .htaccess, add the new rule carefully and test immediately. And if you’re unsure, keep a backup copy (the tool’s “Save as TXT” button makes that habit easy).
Loops usually come from conflicting rules. For example, one rule forces www, another removes www, or an HTTPS rule interacts with host normalization in the wrong order. Plugins and CMS-generated rules can also compete with your manual rules.
The fix is to simplify: ensure there is one clear canonical redirect, place it early, and temporarily disable competing redirects to isolate the cause. Then reintroduce other rules one at a time while testing.
It’s the best place when Apache is your web server and .htaccess is what you can control. However, if you manage the main server configuration (like Apache vhost settings) or you use a CDN/edge layer, implementing redirects there can be faster and easier to maintain.
That said, for many shared hosting setups, .htaccess is the realistic option. In those environments, this htaccess redirect generator is a quick way to generate safe baseline code you can test and deploy immediately.
Why Choose Htaccess Redirect?
This tool is for a very specific job: generate canonical htaccess redirect rules for www and non-www so you stop running two URL versions of the same site. It’s the kind of fix you do once and then forget—because everything just behaves.
You enter a domain, pick the direction, click Get Your HtAccess Code, and copy the result. No hunting for random snippets, no mixing rewrite patterns, and no “I think this is right” uncertainty. Plus, you can save the code as a TXT file so you can document what changed.
If you want a clean canonical host and fewer redirect headaches, use this htaccess redirect generator to lock your site to one version and move on to more interesting problems.