IIS 7 Rewrite web.config serverVariables directive not working in sub folder

[亡魂溺海] 提交于 2020-01-16 05:08:25

问题


I have the following code in my web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="IP Correction">
                    <match url="(.*)" />
                    <serverVariables>
                        <set name="REMOTE_ADDR" value="{HTTP_X-Forwarded-For}"/>
                    </serverVariables>
                    <action type="None" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

This works perfectly on the root of my site, however, the rule isn't being triggered in any of the sub folders.


回答1:


I figured this out. The problem was in this line of code

<action type="None" />

You have to specify the rewrite action

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="IP Correction">
                    <match url="(.*)" ignoreCase="true" />
                    <serverVariables>
                        <set name="REMOTE_ADDR" value="{HTTP_X-Forwarded-For}" replace="true"/>
                    </serverVariables>
                    <action type="Rewrite" url="{R:0}" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>



回答2:


I faced a similar issue and created an IHttpModule to address it, which you can find here. URL Rewrite seems to have a bug where it won't execute on default document requests. The module doesn't have that issue. To implement it on your site, you'd add it to the <modules> section of your web.config, or if you want it to run server-wide, to your applicationHost.config.

The relevant bit of code is that you're hooking into HttpApplication's BeginRequest event, and running:

void OnBeginRequest(object sender, EventArgs e)
{
        HttpApplication app = (HttpApplication)sender;

        string headervalue = app.Context.Request.Headers["X-Forwarded-For"];

        if (headervalue != null)
        {
                Match m = REGEX_FIRST_IP.Match(headervalue);

                if (m.Success)
                {
                        app.Context.Request.ServerVariables["REMOTE_ADDR"] = m.Groups[1].Value;
                        app.Context.Request.ServerVariables["REMOTE_HOST"] = m.Groups[1].Value;
                }
        }
}

The Regex is ^\s*(\d+\.\d+\.\d+\.\d+). Full code at the gist.

If you compiled this code into a class library called HttpModules and put it in your GAC, you could then add this to your <modules> section, something like:

<add name="ClientIP" type="YourLibrary.ClientIP, YourLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=00DEADBEEF00" />



来源:https://stackoverflow.com/questions/15189445/iis-7-rewrite-web-config-servervariables-directive-not-working-in-sub-folder

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