How to Redirect non www to www URLs in Linux & Windows Servers

Websites do not have the www prefix before the domain can be forced to use the www prefix, for example; from example.com to www.example.com. We can see that many of the domains over the internet have a www prefix and many are not.

Redirect non www to www URLs in Linux and Windows Servers

This is not an issue. With www and without www both are fine but it is a common thought that forcing one URL format is better for search engine optimization.

Redirect Non www URLs in Linux Server

To redirect URLs to www, you need to make some changes in your .htaccess file in the root folder. Locate the .htaccess file in your cPanel account and update the following line of codes.

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

Or

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

Note: If you want to redirect http to https pages use following rules. 

RewriteEngine On
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301, L]

Redirect Non www URLs in Windows Server (IIS Server)

You will not find .htaccess file in Windows (IIS) server. Search web.config file and make the following rule changes.

<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name=”Redirect to www” stopProcessing=”true”>
<match url=”.*” />
<conditions>
<add input=”{HTTP_HOST}” pattern=”^example.com$” />
</conditions>
<action type=”Redirect” url=”http://www.example.com/{R:0}”
redirectType=”Permanent” />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Note: Use HTTPS or HTTP in the URL according to, what your site supports.

After making changes in your .htaccess or web.config file, you will see that all non-www URLs will be redirected to www URLs.

Pawan Purohit
Pawan Purohit

I love to share my knowledge with others. I generally write on Tech, Blogging, How to, SEO, etc.

We will be happy to hear your thoughts

      Leave a reply

      Pawan Purohit
      Logo