【问题标题】:iis url rewrite with query stringiis url用查询字符串重写
【发布时间】:2020-10-08 01:43:28
【问题描述】:

请问如何改写网址

“https://hello.com/test/api/zoo/v1/animal?animal_record_no=20900016431”

“https://145.123.2.12:5000/api/zoo/v1/animal?animal_record_no=20900016431”

查询字符串“animal_record_no”是可选的。

基本上我只是想从 url 中删除“测试”并更改 ip 地址。

这是我现在拥有的,但它不转发查询字符串:

                <rule name="ReverseProxyInboundRule3" stopProcessing="true">
                    <match url="^test/([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)/([_0-9a-zA-Z-]+)/?$" />
                    <action type="Rewrite" url="http://145.123.2.12:5000/{R:1}/{R:2}/{R:3}/{R:4}" />
                    <conditions>
                    </conditions>
                </rule>

如果我使用重定向,我会收到 HTTP/1.1 301 Moved Permanently 错误

<rule name="ReverseProxyInboundRule3" enabled="true" stopProcessing="true">                     <match url=".*test/(.*)" />                        
<conditions>                         
<add input="{HTTP_HOST}" pattern="iex.odp.gov.sg" />                    
</conditions>                   
<action type="Redirect" url="http://145.123.2.12:5000/{R:1}" /> 

感谢您的帮助。

【问题讨论】:

    标签: iis url-rewriting


    【解决方案1】:

    如下添加 URL 重写:

    我们不需要为查询字符串添加规则,我们只需要启用“追加查询字符串”,IIS会帮助我们重写查询字符串。

    web.config中的配置如下:

           <rewrite>
                <rules>
                    <rule name="Test">
                        <match url="(.*)(test)/(api)/(zoo)/(v1)/(animal)" />
                        <action type="Rewrite" url="https://145.123.2.12:5000/{R:3}/{R:4}/{R:5}/{R:6}" appendQueryString="true" />
                    </rule>
                </rules>
            </rewrite>
    

    如果两个ULR没有指向同一个主机,还需要开启反向代理。

    如果问题仍然存在,请随时告诉我。

    【讨论】:

    • 非常感谢丁鹏。我没有试过这个。如果我需要帮助,我会回来的。让我先将其标记为答案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 2013-10-10
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多