Htaccess Redirect

Generate Htaccess Redirect code to force one preferred domain version.

test2
Remove Ads

Htaccess Redirect Generator

An Htaccess Redirect tool creates redirect rules for one preferred version of a domain. On this page, you enter a domain name, choose either a redirect from www to non-www or from non-www to www, and generate code you can place in an Apache .htaccess file when you want visitors and search engines to reach a single host version of your site.

This makes the tool most useful when the same site can be reached through both hostnames and you want one canonical destination. Google recommends choosing one preferred URL version and using redirects to send traffic from the alternate version to that preferred destination, while Apache documents that .htaccess is a per-directory configuration method for servers that use it.

How to Generate an Htaccess Redirect

  1. Enter your domain URL.
  2. Select either Redirect From www to non-www or Redirect From non-www to www.
  3. Click Get Your HtAccess Code.

301 Redirect Htaccess Rules for Preferred Domain Control

If your site responds on both www.example.com and example.com, pick one version and redirect the other. Google states that when people can access the same content through several URLs, it is a good idea to choose a preferred canonical destination and use redirects, and permanent redirects help signal that the target URL should be treated as canonical in search.

For this tool, the decision is narrow and practical: do you want the site to resolve to the www version or the non-www version? Once you decide, keep that same choice consistent in internal links, canonical tags, and sitemaps so the redirect is supported by the rest of the site’s signals.

What to Do After You Generate the Code

Generated code is only the first step. Apache explains that .htaccess directives apply on a per-directory basis, and it also notes that main server configuration is preferable when you have access to it because it performs better than repeated .htaccess lookups on every request.

After adding the redirect, test both host versions, confirm that the preferred version resolves correctly, and update the rest of your site so it points to the same destination. Google recommends server-side permanent redirects when possible, advises against redirect chains, and also recommends aligning redirects with canonical annotations, internal links, and sitemap URLs.

Common .htaccess 301 Redirect Mistakes

  • Using a temporary redirect when the host preference is permanent.
  • Keeping internal links or canonical tags pointed at the non-preferred host.
  • Creating redirect chains instead of sending requests straight to the final destination.
  • Editing rules without checking the directory context in which .htaccess is being applied.

Those issues matter because permanent and temporary redirects are handled differently, canonical signals should stay aligned, and Apache rewrite behavior changes in .htaccess because rules are interpreted relative to the current directory rather than the full requested URI.

Example: Choosing a Canonical Host for a Live Site

Your website is accessible at both www.yourdomain.com and yourdomain.com, and both versions are already being shared externally. You decide that the non-www version will be the canonical host. In that case, you would generate the www-to-non-www rule, place it in the correct Apache redirect context, then update canonical tags, sitemap entries, and internal links to the non-www version. The expected outcome is one preferred hostname for users and search engines instead of split signals across two host variants.

Htaccess Redirect FAQs

How do I redirect non-www to www in htaccess?

Use the option that redirects from non-www to www, generate the code, and add it to the Apache redirect location you use for your site. This page is designed specifically around that host-level choice and the opposite www-to-non-www option.

How do I redirect www to non-www in htaccess?

Select the www-to-non-www option, generate the rule, and apply it in the correct Apache configuration context. After that, make sure the non-www version is also the version used in your internal links, canonical tags, and sitemap.

Should I use a 301 or 302 redirect for a preferred domain?

Use a permanent redirect when the change is meant to stay in place. Google says permanent redirects show the new target in search results and use the redirect as a canonicalization signal, while temporary redirects keep the source page as the version more likely to remain shown in search.

Where should I put the redirect code in .htaccess?

It needs to go in the Apache configuration context that controls the part of the site you want to affect. Apache documents that .htaccess works on a per-directory basis and also notes that main server configuration is better when you have access to it.

Do I still need canonical tags after adding a host redirect?

Yes, it is still a good practice to keep canonical tags, internal links, and sitemap URLs aligned with the preferred destination. Google recommends keeping those signals consistent after redirects are active.