How to Redirect non-www to www URLs

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.

Why Redirect non-www to www

Before getting into the technical details it is important to understand why this redirection is necessary:

  1. Search engines view www and non-www versions of your domain as separate entities. Without proper redirection, your SEO efforts could be split between these two versions reducing overall effectiveness.
  2. Redirecting ensures that users always end up on the same version of your website which provides a consistent experience.
  3. Search engines penalize sites that have duplicate content. Redirecting helps to avoid this issue by combining your content under one URL.

Methods to Redirect non-www to www

Following are methods described below to redirect non-www to www URL on different web servers.

Apache .htaccess

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:

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.

.htaccess file

Add the following redirection code:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
apache conf
  • RewriteEngine On: Enables the rewrite engine.
  • RewriteCond %{HTTP_HOST} ^example.com [NC]: Checks if the host does not start with www.
  • RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]: Redirects all requests to the www version with a 301 permanent redirect.

NGINX Configuration

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.

Add the following redirection code:

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
}
nginx conf

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.

Using Microsoft IIS

For websites hosted on Microsoft IIS the web.config file can be used:

Open the web.config which is located in the root directory of your website.

Add the following redirection code:

<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.

Using PHP

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:

<?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.

Using Cloudflare

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:

  • URL pattern: example.com/*
  • Forwarding URL: 301 Permanent Redirect to http://www.example.com/$1

4. Save and Deploy: Activate the rule to start redirecting traffic.

Best Practices

Following are some best practices while redirecting non-www to www URLs.

  • Use 301 Permanent Redirects which ensures that search engines understand that the non-www URL is permanently moved to the www version.
  • Implement redirects carefully and test them to avoid any unintended issues.
  • Regularly monitor your website’s redirects to ensure they are working correctly.
  • If you have a mobile-specific domain implement appropriate redirects to optimize the mobile user experience.

Conclusion

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.

FAQ

What does redirecting non-www to www mean?
Why should I redirect non-www to www?
How can I redirect non-www to www using .htaccess?
Can I redirect non-www to www on a server without .htaccess?
Will redirecting non-www to www affect my website’s SEO?
Do I need a separate SSL certificate for www and non-www?
How can I check if my non-www to www redirect is working?

Related Post

How to Display Related Posts in WordPress

Engaging your readers and keeping them on your WordPres...

How to Install Java on Windows

Java is a powerful programming language that is widely ...

How To Check And Update Git Version

Git is a free and open-source distributed version contr...

How to Fix Duplicate Title Tags in WordPress

Duplicate title tags can be unfavorable for SEO stands ...

How To Make an HTTP Request in JavaScript

The widely used web programming language JavaScript giv...

How to Create Custom Error Pages in cPanel

Ever encountered a generic messages page while browsing...

Leave a Comment