Search Our Database
How to redirect non-www to www using web.config on Windows Server
Introduction
This guide explains how to redirect non-www URLs to www for websites hosted on a Windows Server using IIS (Internet Information Services). It is particularly useful for website administrators and developers who need to ensure that all traffic is consistently directed to the www version of their site, which can help with SEO and avoid duplicate content issues. If you’re using a different platform like WAMP or Apache, this guide is not applicable as those platforms do not use the web.config file for URL rewriting.
Prerequisites
- Access to the Windows Server hosting your website.
- IIS (Internet Information Services) is used to host the website.
- Basic knowledge of editing web.config files.
Step-by-Step Guide
- Open the Web.configfile with a text editor.
- Insert the following code inside the <system.webServer>
- The web.config file is typically located in the root directory of your website C:\inetpub\wwwroot\yourwebsite
<system.webServer> <rewrite> <rules> <clear /> <rule name="WWW Rewrite" enabled="true"> <match url="(.*)" /> <conditions> <add input="{HTTP_HOST}" negate="true" pattern="www\.(.*)" /> </conditions> <action type="Redirect" url="http://{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer>
Verify that the changes have taken effect by doing the following:
- Open a web browser and navigate to your website using a non-www URL http://yourwebsite.com
- If the changes were applied successfully, you should be automatically redirected to the www version of the URL https://www.yourwebsite.com
Conclusion
By following these steps, you’ve successfully set up a permanent redirection from non-www to www URLs on your website hosted through IIS on a Windows Server. This configuration helps maintain consistency in your URL structure and can positively impact SEO.
For additional assistance or if you encounter any issues, please contact our support team at support@ipserverone.com.