How do I redirect a specific port in the IIS server to an other port

喜你入骨 提交于 2021-01-29 10:00:34

问题


My URL Rewrite rule only works for port 80 in IIS .

The rewrite works for : http://localhost:80 --> http://localhost:8100

The rewrite works for : http://localhost:80/redirect --> http://localhost:8100

The rewrite doesnt work for : http://localhost:8080 --> http://localhost:8100

The rewrite doesnt work for : http://localhost:8080/redirect --> http://localhost:8100

My web.config is following:

<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
<system.webServer>
    <directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
    <rewrite>
        <rules>
            <rule name="Redirect to port 8100" stopProcessing="true">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{SERVER_PORT}" pattern="^8100$" negate="true" />
                </conditions>
                <action type="Redirect" url="http://{HTTP_HOST}:8100/{R:0}" />
            </rule>
        </rules>
    </rewrite>
 </system.webServer>
</configuration>

The web.config is only working on default http port 80/443 but not for an specific port.

Which changes has to be made in the web.config for working with all ports?


回答1:


The main problem is the variable {HTTP_HOST} which already contains the PORT information.

To solve this issue you need to adjust two things:

  1. Extend the matching pattern within
  2. Set the correct address for the redirect

I adjusted your web.config to see how it should work with your example:

<?xml version="1.0" encoding="UTF-8"?>
 <configuration>
<system.webServer>
    <directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
    <rewrite>
        <rules>
            <rule name="Redirect to port 8100" stopProcessing="true">
                <match url="(.*)" ignoreCase="true" />

                <conditions logicalGrouping="MatchAll" trackAllCaptures="true" >
                    <add input="{HTTP_HOST}" pattern="([^/:]*?):[^/]*?" />
                </conditions>

                <action type="Redirect" url="http://{C:1}:8100/{R:0}" appendQueryString="false" />

            </rule>
        </rules>
    </rewrite>
 </system.webServer>
</configuration>


来源:https://stackoverflow.com/questions/52040903/how-do-i-redirect-a-specific-port-in-the-iis-server-to-an-other-port

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!