Htaccess Redirect Generator for WWW and non-WWW Versions

Generate htaccess redirect directives for your site in order to redirect www version to non-www version and vice versa.

Htaccess Redirect Generator for WWW and Non-WWW Versions

1. Select redirect type

2. Enter your domain name

Do not include www. Domain name only - e.g.

3. Get your code

4. Copy the code to your .htaccess file

About Htaccess Redirect Generator for WWW and Non-WWW Versions

What is htaccess File?

Htaccess is a server configuration file that you can use to control the behavior of your website. There are many practical use of htaccess file like below:

  1. Setup different types of redirects on your site.
  2. Block bad robots and IP addresses.
  3. Add security and caching directives.

Remember, htaccess is a file extension with no file name, you should see the file as .htaccess on your server. Since, many server and FTP software hide the extensions, you may need to unhide the file by enabling view all hidden files option.

.htaccess Redirect Generator

As mentioned, one of the main ways you can use .htaccess is to setup redirects on your domain. Here is a free htaccess redirects generator tool to create redirect entries for your site for redirecting www version to non-www version and vice versa.

How to Use htaccess Redirect Generator Tool?

There are four steps in using this tool:

  • Choose the redirect type either from www to non-www or from non-www to www.
  • Enter your domain without www or https protocol.
  • Click on the "Get .htaccess Code" button. The tool will instantly generate the redirect code for your selection.
  • Copy the code and paste on your .htaccess file using FTP or File Manager.

Check in browser that both www and non-www versions of your site are pointing to the correct site. Ensure to submit XML Sitemap for the correct version of your site in Google Search Console.

WWW and non-WWW Redirection Code

Here is an example code for redirecting WWW to non-WWW version of the domain

RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

Similarly, here is a code for directing non-WWW to WWW version.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]

As you can see the tool will generate the redirect with HTTP protocol like or Make sure to change it to https:// if you are using secured HTTPS protocol.

Note that this is permanent redirection with HTTP status code 301. This will instruct the browser the page is permanently moved to the other version.

Add Entries in htaccess File Using File Manager

There are two popular ways to modify your htaccess file. First option is to use the File Manager App from your hosting account.

  1. Login to your hosting account and go to cPanel. Note, your hosting provider may offer custom interface instead of cPanel. For example, if you are using SiteGround hosting then you need to "Site Tools" interface.
  2. Search and find the "File Manager" app.
  3. You can find the htaccess file in the root of your site's installation path.
  4. Select .htaccess file and click on the "Edit" button.
  5. Paste the redirection code at the start of htaccess file.
  6. Save your changes.

Add Entries in htaccess File Using FTP

Next option is to use File Transfer Protocol (FTP) to modify the file remotely.

  1. Launch your FTP client like FileZilla and connect to your web server.
  2. Locate the .htaccess file in the root of your site's installation.
  3. Download the htaccess file and paste the entries at the start of the file.
  4. Save your changes and then re-upload it back to your server.

htaccess for Subdomain or Subdirectory

Sometimes, you may have your website installed in a subdomain or in a subdirectory. Generally, .htaccess redirects work recursively and affects all down the level directories. For example, if you have added the above redirect code in "/public_html/.htaccess" file, it will also affect "/public_html/directory" folder. However, you can create a separate .htaccess file for each directory or folder on your server to change the behavior.

Related Tools



WebNots is a digital publishing platform offering tech tips and web tutorials. The objective of the site is to help people creating optimized websites and assist in solving technology related issues. We have published 3000+ free articles for the benefit of webmasters and tech community.