Tsohost Knowledge Base

Web.config - Windows Rewrite Rules

Modified Wednesday 1st July, 2015 at 03:17

Was this helpful?

Rewrite Rules

These rules apply to IIS8 (Windows 2012) on the cloud hosting platform. They cannot be used on Windows 2003/IIS6.

Rewrite rules need to be added to the <system.webServer> section of the web.config file (so that’s after the opening tag for that. After making any changes, make sure to recycle the application pool for that to take effect. 

 

Force SSL on the Cloud

<rewrite>

<rules>

<rule name="SSL" stopProcessing="true">

<match url="^(.*)$"  />

<conditions>

<add input="{HTTP_X_FORWARDED_PROTO}" pattern="https" negate="true"/>

</conditions>

<action type="Redirect" redirectType="Found" url="https://{HTTP_HOST}/{R:1}" />

</rule>

</rules>

</rewrite>

Like .htaccess, this checks for the presence of HTTP_X_FORWARDED_PROTO and then rewrites everything through to the https:// version of the site.

Rewrite from non-www to www

<rewrite>

<rules>

<rule name="Canonical Host Name" stopProcessing="true">

 <match url="(.*)" />

 <conditions>

   <add input="{HTTP_HOST}" negate="true" pattern="^www.yoursite\.com$" />

 </conditions>

 <action type="Redirect" url="http://www.yoursite.com/{R:1}" redirectType="Permanent" />

</rule>

</rules>

</rewrite>


Redirect a specific URL or page

<rewrite>

<rule name="Redirect Old Page" stopProcessing="true">

  <match url="old-page.asp" />

  <action type="Redirect" url="http://www.domain.com/new-page" redirectType="Permanent" />

</rule>

</rewrite>

Still can't find what you're looking for?

That's not a problem. Our 24/7 customer support team are friendly and waiting to help answer your questions. The easiest way to get in touch is to raise a support ticket; simply click the button below.

Open Support Ticket