Redirect Non WWW URL To WWW Domain In Linux and Windows

Learn how to redirect non www URL to www URL, like to We can see that many domains over the internet are having www prefix and many are not.

This is not an issue actually. 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 URL in Linux server:

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

RewriteEngine On
RewriteCond %{HTTP_HOST} ^ [NC]
RewriteRule ^(.*)$$1 [L,R=301]


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

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

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

Redirect non www URL to www domain in windows server:

You will not find .htaccess file in windows server. find your web.config file and make the following rule changes.

<rule name=”Redirect to www” stopProcessing=”true”>
<match url=”.*” />
<add input=”{HTTP_HOST}” pattern=”^$” />
<action type=”Redirect” url=”{R:0}”
redirectType=”Permanent” />

Note: Use https or http in URL acordning to you, what your site supports.

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

Leave a Reply