1. Home
  2. Cloud & Dedicated Servers
  3. Scripts and Programming
  4. How do I redirect non-www requests to www requests?

How do I redirect non-www requests to www requests?

 

All websites are setup such that http://www.domain.com and http://domain.com both direct to your website content. This is done because many visitors tend to type both urls in their browser, however some search engines may actually penalize you should they spider your site via both URLs. Fortunately, Applied Innovations has a solution.

All hosting accounts have ISAPI_Rewrite enabled and included by default. ISAPI_Rewrite is a powerful URL rewriting engine created by www.helicontech.com and full details on it’s use can be found at the developers website.

 

ISAPI Rewrite Version 3

(ISAPI Rewrite Version 3 Documentation – http://www.helicontech.com/isapi_rewrite/)

 

To get around the above problem first create a file in your wwwroot (web root folder) named .htaccess and in that file put the following contents:

 RewriteEngine on RewriteCond %{HTTPS} (on)? RewriteCond %{HTTP:Host} ^(?!www.)(.+)$ [NC] RewriteCond %{REQUEST_URI} (.+) RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

 

Excluding a Dedicated IP from non-www to www redirect

If for any reason your need to view your site over a dedicated IP you can exclude it from being redirected to www. For example using the code above for http://127.0.0.1/ would cause the site to redirect to http://www.127.0.0.1/ which will return an invalid URL error.

Here’s how to modify the ISAPI code:

 RewriteEngine on RewriteCond %{HTTPS} (on)? RewriteCond %{HTTP:Host} ^(?!(?:www.|d))(.+)$ [NC] RewriteCond %{REQUEST_URI} (.+) RewriteRule .? http(?%1s)://www.%2%3 [R=301,L]

The insertion of the d ensures that the rule applies only if there are no digits in the HTTP:Host (bear in mind that you do not want to use this version if your site contains any numbers).

 

ISAPI Rewrite Version 2

(ISAPI Rewrite Version 2 Documentation – http://www.isapirewrite.com/)

 

The above examples are intended for servers running ISAPI_Rewrite v3. The following example is intended for servers running ISAPI_Rewrite v2. First create a file in your wwwroot (web root folder) named httpd.ini and in that file put the following contents:

 [ISAPI_Rewrite]
 RewriteCond Host: mysite.com RewriteRule (.*) http://www.mysite.com$1 [I,RP]

Be sure to replace mysite and com with your actual website domain and domain TLD, noting that the must prefix any periods in the RewriteCond Host rule. Save this file and your site should now automatically create a 301 permanent redirection saving your site from becoming ‘supplemental content’. All new accounts get provisioned on servers running version 3 but if you are on an older server it’s possible that you may have to use the version 2 example. If you would like to verify what version of ISAPI_Rewrite is being used on your server please send an email to support@appliedi.net and include your domain name.

 

 

 

Content retrieved from: https://support.appliedi.net/kb/a395/how-do-i-redirect-non-www-requests-to-www-requests.aspx.

Updated on November 11, 2019
Was this article helpful?

Related Articles

Customer Support Portal
Can't find the answer you're looking for? Don't worry we're here to help!
CONTACT SUPPORT