IIS URL Rewrite
From MgmtWiki
Contents
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.
- Open Internet Information services (IIS) manager (for example from administrative tools)
- Click on the Server in the left pane (click a second time if you don't see sites)
- Click on sites
- 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)
- 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.
- Click "Add Rule" in the right pane.
- In the Add Rules dialog box, select Blank Rule and click OK.
- Give the rule a name like "https fix"
- In Requested URL: Matches the Pattern
- In the Pattern Text box enter "(.*)" <-- matches every url
- Logic grouping: Match all Input {HTTPS} TYpe Matches the Pattern Pattern: ^ON$ <-- only applies to HTTPS
- Action Type: Redirect
- Action Properties Recired URL: http://{HTTP_HOST}:8765/{R:2} <-- should be a different site (a different port is one)
- Append query string (probably) check this
- Redirect type: Permanent (301)
- Most importantly - remember to click "apply" in the right pane
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>