Help Docs Server Administration Linux Server Administration Apache Web Server How to redirect non-WWW to WWW in the HTACCESS file

How to redirect non-WWW to WWW in the HTACCESS file

Learn how to redirect non-WWW URLs to WWW using .htaccess on Nexcess servers. This guide includes specific instructions for WordPress, Magento, and standard 301 redirects.

In the early days of the Internet — the World Wide Web — every website’s domain name had “WWW” prepended to it. While the plain domain or example.com is more common than www.example.com, is there a particular benefit to selecting one choice over the other?

If you’re a business owner, dropping “WWW” is often recommended for branding. You want your domain name to be clear of extraneous and unrelated characters. Not to mention, the bare domain (or non-WWW version of your domain) looks better on marketing materials and feels easier to remember. Whichever direction you go, make sure that the website visitors that open the door to your content via one URL are directed to the proper final destination URL — the version of your domain name’s URL that you have your official homepage.

Redirecting non-WWW to WWW in the .htaccess file

If you’re hosting your website with Nexcess, you can redirect non-WWW to WWW URL via .htaccess since Nexcess servers use Apache Web Servers.

WordPress admins can access the .htaccess file in the site’s root directory via an FTP client or an SSH connection.

After accessing it, you can add the following code to the file to redirect non-WWW URLs to www ones:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

Make sure to replace example.com with your actual domain name.

If you’re just trying to force all visitors going to the non-WWW version of your site’s web address to go to the WWW subdomain, you’ll need to add one of the following rules to the .htaccess file in your website’s document root directory:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Redirecting non-WWW to WWW in WordPress-based websites

If you own a WordPress blog or a WooCommerce store, you’ll want to ensure all the links in the database are using the correct version of your site’s domain name.

Regardless of if you choose the WWW version or the non-WWW version, you will not want to have both versions in the database. The quickest way to update the links in the database is to use the wp search-replace command using WP-CLI:

wp search-replace 'http://example.com' 'http://www.example.com'
wp search-replace 'https://example.com' 'https://www.example.com'

For more information, check out WP-CLI commands for your WordPress database in our online resources. If you don’t want to bother with WP-CLI, you could also use the Better Search Replace plugin. Once you’ve updated the links in the database, you can add the redirect in the .htaccess file, as previously described.

Redirecting non-WWW to WWW in Magento-based websites

If your ecommerce website is Magento-based, ensure your database lists the correct base URLs. You can change the base URL in Magento from Admin > Stores > Settings > Configuration:

  • Base URL — https://www.example.com/
  • Secure Base URL — https://www.example.com/

You can also do the same actions via Magento CLI by executing the following commands:

php bin/magento setup:store-config:set --base-url="https://www.example.com/"
php bin/magento setup:store-config:set --base-url-secure="https://www.example.com/"

After amending the base URLs, you can configure the .htaccess file as shown above via an FTP or SSH connection.

Redirecting one URL to another URL via the .htaccess file

The most widely used technique on websites for redirecting a URL is done via the .htaccess file. In addition, it helps you configure many global settings of your website. An Apache Web Server with an activated mod_rewrite module is a prerequisite because the redirection code is entered using the website’s root directory.

On most Nexcess web hosting servers (Apache-based), you can find the .htaccess file in the website’s root folder. You can also make one yourself if you can’t find one.

After accessing .htaccess, you can redirect a URL to another URL by adding the appropriate code. The basic full domain redirect would look like this:

redirect 301 / https://www.example.com/

If you’d like to redirect just a subpage of your website, you’d need to use a redirect similar to this:

redirect 301 /old.html https://www.example.com/new

Another example would be:

Redirect 301 /old.html https://www.example.com/new.html

Make sure to replace old.html and new.html with actual web addresses. For example, consider a 2022 statistics round-up page with a URL of https://www.example.com/2022-best-stats/ which will appear aged in 2023. Instead, you can direct incoming traffic of that statistics round-up page to another statistics round-up page with a URL of https://www.example.com/latest-best-stats without the year in the URL character string.

In our example, you can implement the redirect in .htaccess file by adding the following code:

Redirect 301 /2022-best-stats https://www.example.com/latest-best-stats

Similarly, you can redirect traffic from your old website to your new website by adding this code in the .htaccess file of the old website:

Redirect 301 / https://www.example.net/

Conclusion

Establishing a preferred domain format—whether “WWW” or non-WWW—is crucial for consistent branding and search engine optimization (SEO). By utilizing the .htaccess file for server-side redirects and updating your application’s database settings, you ensure that all visitor traffic is routed correctly. Whether managing a WordPress blog or a Magento store, these steps prevent duplicate content issues and guarantee a professional, seamless experience for your users.

Was this article helpful?