How do I use web.config to redirect to a query string on Windows Server 2008 R2?

点点圈 提交于 2019-12-11 18:15:36

问题


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:


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>


来源:https://stackoverflow.com/questions/22181429/how-do-i-use-web-config-to-redirect-to-a-query-string-on-windows-server-2008-r2

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