我的 URL 重写规则仅适用于 IIS 中的端口 80。
重写适用于:http://localhost:80 --> http://localhost:8100
重写适用于:http://localhost:80/redirect --> http://localhost:8100
重写不适用于:http://localhost:8080 --> http://localhost:8100
重写不适用于:http://localhost:8080/redirect --> http://localhost:8100
我的 web.config 如下:
<?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.
必须在 web.config 中进行哪些更改才能使用所有端口?
最佳答案
主要问题是变量{HTTP_HOST}
,它已经包含PORT
信息。
要解决此问题,您需要调整两件事:
- 扩展匹配模式
- 设置正确的重定向地址
我调整了您的 web.config
以查看它如何与您的示例配合使用:
<?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>
关于redirect - 如何将 IIS 服务器中的特定端口重定向到其他端口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52040903/