How do I use web.config to redirect to a query str

2019-08-30 01:37发布

I've got a redirect I want to do using web.config on an IIS 7.5 server running Windows Server 2008 R2. I'd like to simply make a shortcut URL for another URL with a very long query string:

www.example.com/redirect -> www.example.com/long_url.aspx?key1=value1&key2=value2

When I add the following rewrite rule to web.config though, it gives a 500 Internal Server Error:

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="^redirect$" stopProcessing="true">
                    <match url="^redirect$" />
                    <action type="Redirect" url="/long_url.aspx?key1=value1&key2=value2" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

What do I need to change to get this to work?

1条回答
乱世女痞
2楼-- · 2019-08-30 02:18

When adding a query string in the action of a rewrite rule, you've got to escape all the "?" and "&" characters in the URL.

  • "?" = "&#63;"
  • "&" = "&amp;"

The following code will work:

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="^redirect$" stopProcessing="true">
                    <match url="^redirect$" />
                    <action type="Redirect" url="/long_url.aspx&#63;key1=value1&amp;key2=value2" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>
查看更多
登录 后发表回答