How to force your site from non www to www using your web.config file

How to force your site from non www to www using your web.config file

If you want to force your site to always load in www, for example, you want when visitors enter URL in the browser with a non www eg example.com and it will automatically load in www.example.com, then you can do so by modifying your web.config file.

Please follow the steps below:

1. Login to your site FTP, go to httpdocs folder.
Check if you have web.config file.

2. If you don't have web.config file, then please create a new file called web.config using notepad and enter this code :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="RedirectNonWwwToWww" 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: Please change the example.com to your domain name.

3. Upload the web.config into your httpdocs folder.

4. If you already have web.config file, then please open the file with notepad or any other text editor and insert this new code before the </configuration> :

<system.webServer>
<rewrite>
<rules>
<rule name="RedirectNonWwwToWww" 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>

NOTE: Please change the example.com to your domain name.

5. Upload the web.config into your httpdocs folder to replace your existing.

Thank you.



 

 


Times Viewed:
1973
Added By:
Wilson Keneshiro
Date Created:
9/11/2020
Last Updated:
9/11/2020