IIS URL Rewrite

From MgmtWiki
Jump to: navigation, search

Full Title

Using Windows Server URL Rewrite Module 2.1 for IIS 7 and above (Server 2012 and above).

Context

  • As of 2019-08-16 the current version of the rewrite module was 2.1.
  • It can be downloaded from here https://www.iis.net/downloads/microsoft/url-rewrite
    • DO NOT click down load at the top as you might get X86 version. To be sure you have the X64 version go down to the detail list and click on X64.
  • Restart IIS (or reboot if you are not sure if your machine is up-to-date.

Example

Goal: Redirect https: requests to a separate port with only http: for a scheme.

  1. Open Internet Information services (IIS) manager (for example from administrative tools)
  2. Click on the Server in the left pane (click a second time if you don't see sites)
  3. Click on sites
  4. Clink on (typically) Default Web Site (the rewrite rules created here will apply to all of the sub-sites, which is probably the place to be)
  5. Double click on "URL Rewrite" in the center pane. - the assumption here is that there are no existing rules, if there are it gets complicated.
  6. Click "Add Rule" in the right pane.
  7. In the Add Rules dialog box, select Blank Rule and click OK.
  8. Give the rule a name like "https fix"
  9. In Requested URL: Matches the Pattern
  10. In the Pattern Text box enter "(.*)" <-- matches every url
  11. Logic grouping: Match all Input {HTTPS} TYpe Matches the Pattern Pattern: ^ON$ <-- only applies to HTTPS
  12. Action Type: Redirect
  13. Action Properties Recired URL: http://{HTTP_HOST}:8765/{R:2} <-- should be a different site (a different port is one)
  14. Append query string (probably) check this
  15. Redirect type: Permanent (301)
  16. Most importantly - remember to click "apply" in the right pane

EditInboundRule.png

Here is what that looks like in the web.config file in the iis root

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="https fix" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Redirect" url="http://{HTTP_HOST}:8765{REQUEST_URI}" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^ON$" />
                    </conditions>
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

References