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.
How to redirect non-WWW to WWW URLs using the hypertext access file — HTACCESS
Let's review how to redirect non-WWW to WWW URLs in using Apache Web Server's hypertext access (.htaccess) file. This guide will take you through what is involved — how to redirect non-WWW to WWW URLs section-by-section:
- How to redirect non-WWW to WWW in the .htaccess file
- How to redirect non-WWW to WWW in WordPress-based websites
- How to redirect non-WWW to WWW in Magento-based website
- How to redirect one URL to another URL via the .htaccess file
Regardless of your chosen domain, you should direct visitors to one of the versions of your site's domain name:
- The non-WWW URL version of your domain name
- Or, the WWW URL version of your domain name
Learn how to redirect non-WWW to WWW in the HTACCESS file with regard to your website's possible main URLs. By modifying the files used to configure Apache HTTP Server, such as the .htaccess file in this case, let's look at what it takes.
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. Check out the Nexcess Knowledge Base to see how you can connect via FTP or SSH.
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. You can always ask Nexcess Support for help If you’re struggling with a step. Our team of experts is ready 24/7/365 to help you deal with technical issues
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. You can access it via an SSH or FTP connection. of course, Oou can find access to relevant credentials using Nexcess Client Portal guide.
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
A reliable business partner is vital for streamlining any activity, regardless of whether you own a blog or an online store.
With fully managed hosting from Nexcess, better is built in...
Working on a complex project? Our experts work with you to build the perfect solution for your business.
Nexcess is powered by Liquid Web, and offers offers powerful hosting solutions for WordPress, WooCommerce, and Magento at affordable prices. With confidence, you can rely on our support staff 24/7/365 to get out of any blockers.
Note about the original publication date
This article was originally published in September 2019. It has since been updated for accuracy and comprehensiveness.