Skip to content

Instantly share code, notes, and snippets.

@developerdizzle
Last active September 18, 2023 08:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save developerdizzle/9695491 to your computer and use it in GitHub Desktop.
Save developerdizzle/9695491 to your computer and use it in GitHub Desktop.
IIS Rewrite Cheat Sheet
<rewrite>
<!-- Have a bunch of redirects? Put them in a separate file -->
<rules configSource="Rewrites.config" />
<rules>
<!-- Simple rewrite -->
<rule name="Simple rewrite" stopProcessing="true">
<match url="^path/sub path/page\.aspx$" />
<action type="Rewrite" url="/newpath.aspx" />
</rule>
<!-- Simple redirect -->
<rule name="Simple redirect" stopProcessing="true">
<match url="^path/sub path/page\.aspx$" />
<action type="Redirect" url="/newpath.aspx" />
</rule>
<!-- Based on query string -->
<rule name="Based on query string" stopProcessing="true">
<match url="^path/sub path/page\.aspx$" />
<conditions>
<add input="{QUERY_STRING}" pattern="^parameter=value$" />
</conditions>
<action type="Redirect" url="/newpage.aspx" appendQueryString="false" />
</rule>
<!-- Based on host name -->
<rule name="Based on host name" stopProcessing="true">
<match url="^path/sub path/page\.aspx$" />
<conditions>
<add input="{HOST_NAME}" pattern="^(www\.)?mydomain\.com" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/newpage.aspx" />
</rule>
<!-- Canonical redirect -->
<rule name="Canonical redirect" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HOST_NAME}" pattern="^www\.mydomain\.com$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:1}" />
</rule>
<!-- Canonical redirect with exception -->
<rule name="Canonical redirect with exception" stopProcessing="true">
<match url="(.*)" />
<conditions logicalGrouping="MatchAll">
<add input="{HOST_NAME}" pattern="^www\.mydomain\.com$" negate="true" />
<add input="{REQUEST_URI}" pattern="^/dont-redirect-me$" negate="true" />
</conditions>
<action type="Redirect" url="http://www.mydomain.com/{R:1}" />
</rule>
<!-- Force HTTPS -->
<rule name="Force HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
   <add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>
<!-- Stop hot-links -->
<rule name="Stop hot-links">
<match url=".*\.(gif|jpg|png)$"/>
<conditions>
<add input="{HTTP_REFERER}" pattern="^$" negate="true" />
<add input="{HTTP_REFERER}" pattern="^http://mydomain\.com/.*$" negate="true" />
</conditions>
<action type="Rewrite" url="/images/troll_face.png" />
</rule>
</rules>
</rewrite>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment