Or copy link
Copy link
Redirecting non-www to www URLs is a common task for system or website administrators, as it ensures that both versions of a website are accessible to users but also helps maintain consistency in website traffic and SEO rankings.
In this post, we will cover how to redirect non-www to www URLs covering several methods across different server environments.
Before getting into the technical details it is important to understand why this redirection is necessary:
Following are methods described below to redirect non-www to www URL on different web servers.
For websites hosted on an Apache server, the .htaccess file can be used to set up a redirection. Here’s how you can do it:
.htaccess
The .htaccess file is located in the root directory of your website. If it doesn’t exist, you can create one. Log in to your web hosting control panel which in my case cPanel. Navigate your website’s root directory.
Add the following redirection code:
RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
For NGINX users, the server configuration file needs to be edited:
First, you need to install NGINX on Ubuntu Linux server. Open the NGINX configuration file which is typically found in /etc/nginx/nginx.conf or /etc/nginx/sites-available/default.
/etc/nginx/nginx.conf
/etc/nginx/sites-available/default
server { listen 80; server_name example.com; return 301 http://www.example.com$request_uri; } server { listen 80; server_name www.example.com; root /var/www/html; # Other configurations }
The first server block captures requests example.com and redirects them to www.example.com. The second server block handles requests to www.example.com.
example.com
www.example.com
For websites hosted on Microsoft IIS the web.config file can be used:
web.config
Open the web.config which is located in the root directory of your website.
<configuration> <system.webServer> <rewrite> <rules> <rule name="Redirect non-www to www" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" pattern="^example.com$" /> </conditions> <action type="Redirect" url="http://www.example.com/{R:1}" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
This rule checks if the request is to the non-www version and redirects it to the www version.
Install Web Servers on Our Cheap Linux VPS!
Enjoy the reliable performance of the best Linux system along with the flexibility of a virtual server. Experience fast speeds and low delays.
If you do not have access to server configurations, you can achieve this redirection using PHP:
Open Your Index.php or Create a New File. Add the following redirection code:
Index.php
<?php if (strpos($_SERVER['HTTP_HOST'], 'www.') === false) { header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www." . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); exit(); } ?>
This script checks if www is missing from the host and performs a 301 redirect to the www version.
www
If you are using Cloudflare you can set up a redirect using Page Rules:
1. Log in to Cloudflare: Access your domain dashboard.
2. Go to Page Rules: Add a new Page Rule.
3. Set the Rule:
example.com/*
301 Permanent Redirect to http://www.example.com/$1
4. Save and Deploy: Activate the rule to start redirecting traffic.
Following are some best practices while redirecting non-www to www URLs.
Redirecting non-www URLs to www URLs is an important step in ensuring your website’s traffic and SEO are optimized. The method you choose will depend on your server environment and access level. Whether you are using Apache, Nginx, IIS, PHP, or a service like Cloudflare the principles remain the same which is to capture the non-www traffic and redirect it easily to the www version. Taking these steps will help combine your website’s authority improve user experience and avoid the issues of duplicate content.
Multiple web server offers flexibility and a wide range of customization options. Ultimately, the ideal web server for you will depend on your project’s specific needs. Rent a VPS server from Ultahost that scales with your resources to accommodate your growing demands. Explore our VPS plans to find the perfect option for you.
It means forwarding all non-www website traffic example.com to the www version www.example.com.
It helps avoid duplicate content issues, improves SEO, and ensures a consistent website URL.
Add specific rewrite rules in your .htaccess file to redirect non-www to www.
Yes, you can use server configurations like Apache, NGINX, or cloud services like Cloudflare.
No, it improves SEO by consolidating traffic and avoiding duplicate content penalties.
No, most SSL certificates cover both www and non-www versions of a domain.
Use your browser or online tools to test if non-www URLs redirect to www properly.
Engaging your readers and keeping them on your WordPres...
Java is a powerful programming language that is widely ...
Git is a free and open-source distributed version contr...
Duplicate title tags can be unfavorable for SEO stands ...
The widely used web programming language JavaScript giv...
Ever encountered a generic messages page while browsing...
Save my name, email, and website in this browser for the next time I comment.
Δ