Difference between revisions of "IIS URL Rewrite"

From MgmtWiki
Jump to: navigation, search
(Example)
(References)
 
(3 intermediate revisions by the same user not shown)
Line 18: Line 18:
 
#Give the rule a name like "https fix"
 
#Give the rule a name like "https fix"
 
# In Requested URL: Matches the Pattern
 
# In Requested URL: Matches the Pattern
#In the Pattern Text box enter "https://(.*)/(.*)"  <-- it would be good to check "test pattern" and try several likely URLs to be sure of result.
+
#In the Pattern Text box enter "(.*)"  <-- matches every url
#check "Ignore case"
+
#Logic grouping: Match all Input {HTTPS} TYpe Matches the Pattern Pattern: ^ON$  <-- only applies to HTTPS
#Logic grouping: Match all Input {HTTPS} TYpe Matches the Pattern Pattern:  
 
 
#Action Type: Redirect
 
#Action Type: Redirect
#Action Properties Recired URL: http://{HTTP_HOST}:8765/{R:2}
+
#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
 
#Append query string (probably) check this
 
#Redirect type: Permanent (301)
 
#Redirect type: Permanent (301)
 
#Most importantly - remember to click "apply" in the right pane
 
#Most importantly - remember to click "apply" in the right pane
 
[[File:EditInboundRule.png]]
 
[[File:EditInboundRule.png]]
 +
 +
Here is what that looks like in the web.config file in the iis root
 +
<PRE>
 +
<?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>
 +
</PRE>
  
 
==References==
 
==References==
 
*[https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-the-url-rewrite-module#url-rewrite-20-features Using the URL Rewrite Module]
 
*[https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/using-the-url-rewrite-module#url-rewrite-20-features Using the URL Rewrite Module]
 +
*[https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-configuration-reference URL rewrite configuration reference]
 
*[https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-video-walkthrough Using the URL rewrite module - video walkthrough]
 
*[https://docs.microsoft.com/en-us/iis/extensions/url-rewrite-module/url-rewrite-module-video-walkthrough Using the URL rewrite module - video walkthrough]
 +
*[https://stackoverflow.com/questions/31016953/what-is-the-meaning-of-on-and-off-in-url-rewrite-pattern-and-what-matches-th What is the meaning of ^ON$ and ^OFF$ in url rewrite pattern and what matches this pattern]
  
 
[[Category:Windows Server]]
 
[[Category:Windows Server]]
 +
[[Category:Best Practice]]

Latest revision as of 12:41, 8 November 2019

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