How to Redirect Non-www to www URLs
As a website owner, you might have wondered whether using a www or non-www website domain is simply a matter of user preference.
Even though a non-www domain may be simpler and easier for your audience to remember, it has some disadvantages compared to a www domain.
A www domain helps avoid both the possibility of split page ranking or split inbound links and any duplicate content when search engines index the site. Thus, it improves your site’s search engine optimization (SEO).
If you use a non-www URL, redirecting it to www may seem intimidating, but it’s not a complicated process. Keep reading, as this article will show you three different methods to redirect non-www to www URLs.
Why Redirect Non-www URLs to www?
Sticking with a non-www domain has some drawbacks, including the lack of overload protection and control over cookies.
Traffic from non-www domains can’t be redirected to another server because they don’t have a CNAME record. Due to that, if your server experiences an overload, you won’t be able to redirect traffic to a healthy server.
Thus, visitors may have a hard time accessing your website, negatively affecting its performance and SEO.
In addition to that, search engines consider https://www.yourdomain.com and http://yourdomain.com different websites. Therefore, they are ranked separately and require distinct SEO strategies.
Also, with a non-www domain, you can’t use a cookie-free subdomain unless you create a separate domain to deliver static content.
On the other hand, a www URL allows you to restrict cookies to the root domain or a specific subdomain, reducing the number of HTTP requests and improving the performance of the website.
Redirecting Non-www URLs to www
There are several ways to redirect non-www URLs to www – via your hosting account’s control panel, a content delivery network (CDN), or web server software.
To avoid losing your ranking power, we recommend performing a permanent 301 redirect instead of a temporary one.
Warning! Before proceeding to the tutorial, make sure you have a backup of your website files. This way, you can restore the site to a previous version if an error occurs during the process.
hPanel
The easiest way of redirecting a non-www URL to www is to place a rule in the .htaccess file. You can do so via FTP, SSH, or your hosting account’s control panel.
hPanel users can easily access and edit the .htaccess file. Here are the steps to do it:
- Log in to your hPanel, scroll down to the Files section, and open the File Manager.
- Navigate to the public_html directory, right-click the .htaccess file, and select Edit. If you can’t find it, refer to our article to learn how to create the .htaccess file for your WordPress site.
- Find the line
RewriteEngine On
and insert the following code after it:RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
- Don’t forget to replace yourdomain.com with your actual domain name. Once you’re done, save the changes.
This .htaccess rule will now redirect all visitors from the non-www to the www version of your website.
cPanel
cPanel users can redirect non-www URLs to www via the Redirect settings or editing the .htaccess file. If you are a beginner, we recommend following the first method:
- Access your cPanel, then navigate to Domains -> Redirects.
- Under Type, select the Permanent (301) option.
- On https?://, enter the domain you want to redirect. Leave the path section (/) empty.
- In the Redirects to field, type in your website’s www URL.
- Select Do Not Redirect www, and uncheck Wild Card Redirect.
- Press the Add button.
If the above method doesn’t work, you can set the redirect manually by editing the .htaccess file:
- From your cPanel dashboard, go to Files -> File Manager.
- Look for the public_html folder and double-click it.
- Right-click the .htaccess file and select Edit.
- Under
RewriteEngine On
, insert the following lines of code:RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
Important! Don’t forget to replace yourdomain.com with your actual domain name.
- Save Changes
Cloudflare
If you’re planning on setting up a CDN like Cloudflare to improve the speed and performance of the site, you will also be able to use it to redirect the non-www to the www domain.
Expert Tip
You may want to check the following tutorials before proceeding:
Cloudflare setup process for WordPress sites
Cloudflare setup tutorial for general websites
Once you’ve created an account, log in, and set up a page rule on your Cloudflare:
- On your Cloudflare dashboard, click on the Rules tab.
- Press the Create Page Rule button.
- Type in your current website URL without the www, then set the page rule as Forwarding URL.
- Select 301 – Permanent Redirect as the status code.
- Type in your website’s URL with the www, then click Save and Deploy.
Wait for the redirect to go live.
NGINX
If you use virtual server hosting and NGINX, follow the tutorial below to redirect non-www URLs to www:
- Log in to your VPS account using an SSH like PuTTY.
- Navigate to the directory /etc/nginx/.
- Type in the command below to view the directory content:
[server]$ sudo ls - la
- Enter your NGINX password.
- Type in the following command to edit the nginx.conf file:
[server]$ sudo nano nginx.conf
- Add the following lines of code, replacing yourdomain.com with your actual domain name:
server {
server_name yourdomain.com;
return 301 $scheme://www.yourdomain.com$request_uri;
}
- Enter the following command to restart the NGINX web server:
sudo systemctl restart Nginx
In addition to redirecting non-www to www URLs, you can also run other redirects through NGINX, such as HTTP to HTTPS and other page redirects.
Pro Tip
Hostinger users can manage their VPS from the new browser-based terminal, eliminating the need for additional software.
Apache
If you’re a VPS and Apache user, you’ll also need to edit the .htaccess file. Just like the previous tutorial, it can be done via an SSH terminal.
Important! Before continuing with the steps below, make sure you have root access with sudo privileges and a text editor like Nano.
By default, Apache doesn’t allow the use of .htaccess file, so the steps will be a little different:
- Enable mod_rewrite by running the following command:
$ sudo a2enmod rewrite
Then, restart the Apache web server:$ sudo systemctl restart apache2
- Enable .htaccess with the command:
$ sudo vi /etc/apache2/sites-available/000-default.conf
Add the following code before the<VirtualHost>
line:<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
Then, restart Apache. - To create the .htaccess file, enter the command:
$ sudo vi /var/www/html/.htaccess
- Add the following lines to the file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
- Restart the Apache server.
All site visitors using the non-www URL should now be redirected to the www version.
Conclusion
People tend to leave the www out when typing a website’s address. It might not cause significant issues since visitors will reach the same page, but it’s best to redirect site visitors to its www version.
This is because your site’s performance and SEO might be affected if you keep using non-www domains.
Though it may sound complicated, the redirect process will only take a minute or two. In this article, you’ve learned how to redirect non-www URLs to www in several ways:
- Via your hosting account’s control panel – hPanel and cPanel.
- Using Cloudflare.
- For VPS users, via NGINX and Apache.
Now, whenever someone accesses your site using http://yourdomain.com, they will be redirected to http://www.yourdomain.com.
We hope this article was helpful. Let us know in the comments below if you have any questions.
Suggested Reading
Check out our tutorial featuring The Best Tools For Website Optimization, ensuring a smoother and more engaging user experience.
Comments
February 12 2020
Thanks!!
August 02 2020
Thanks for the insight! Looking to do exactly this. I need to get my cdn working, and www is necessary for that. One question I have is in addition to www, I need it to go to https. Would I just add the https in place of the http?
October 14 2020
I am using this, but it is not working. The site is not loading up. Could you please help.
February 02 2021
Hi, Omar! If you're still having any issues with redirect to www, don't hesitate to contact our Customer Success team to help you troubleshoot. They'll be happy to assist on your case ;)
February 17 2021
VERY SIMPLE EXPLANATION!!! Any beginner can follow these steps.
May 06 2021
Heya Janeesh! Thanks :)
June 12 2021
Thank you sir it's worked for me!
October 15 2021
Hi there - I get an error - too many redirects? Is there something in Wordpress that automatically redirects from www to root directory perhaps? Thanks!
October 19 2021
Hi, too many redirects error generally means that the website has been redirected too many times. I'd suggest to start by looking into your .htaccess code - make sure you only have one redirect there. Then move on to the database and other methods and make sure you only have the correct URL there (e.g. https://www.domain.com)
December 18 2021
Awesome :)
February 13 2022
Hi, everyone! Nice post, but : What will happened if you need the headers from non-www, and then you redirect to www. ? :) Answer: All the headers will be lost cuz of the redirect. So please be careful with the redirects :) Have a nice day :)
February 27 2022
Where in the nginx conf file should you put it? I added it after the HTTP section and now my nginx server wont work or restart....
March 01 2022
Hi, you can add it at the very end of your file, it should do the trick - but do make sure not to remove any of the existing code inside of it.
August 14 2023
Am I supposed to keep my A-record pointing the base domain to the server's IP address?
August 18 2023
Hello there! Basically A record must match with the server IP address that the website is hosted on.
June 24 2024
You have well explained everything. But can you tell how to do vice versa of it? Because I have a website "Filp a Coin Online" and it is without www. I want that when someone searches with www, it should redirect to non-www version of my website.
June 27 2024
Thank you for your feedback! While we understand your preference for non-www, it's important to note that www URLs generally offer better security practices ;)
August 18 2024
I was directed to use the "www" option in the hostinger settings...?
August 21 2024
Hello! Using the "www" option in Hostinger settings helps ensure your website always loads with "www" in the address, like "www.example.com." This not only keeps your website consistent but also makes it more secure by avoiding potential conflicts with non-www versions of your site. It's a good practice for better SEO and overall security ;)