IIS重写总是触发相同的规则

时间:2019-11-14 09:36:30

标签: iis url-rewriting url-routing

我在配置IIS进行URL重写时遇到问题。

我在tomcat上使用端口1512运行IIS,在IIS 80上运行IIS,我想捕获两种不同的情况:

1)在没有任何附加组件myserver:80 /的情况下进入端口80的呼叫应路由到myserver:1512 / pim / webaccess

2)网址中带有pim的调用myserver:80 / pim / scripts / script.js,应路由到myserver:1512 / pim / scripts / script.js

我已经设置了如下两条规则,但是运行此规则时,它总是会触发上面示例1中的一条规则,导致返回登录页面而不是例如js文件。不确定我在做什么错吗?

链接到例如脚本文件在登录页面中的格式为/pim/scripts/script.js可以与它有关吗?

现在真的很沮丧:P

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <httpRedirect enabled="false" destination="http://myserver123:1512/pim/webaccess" exactDestination="true" />
        <rewrite>
            <rules>
                <clear />
                <rule name="UrlWithPimRewrite" stopProcessing="true">
                    <match url="/pim.*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <serverVariables>
                        <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
                        <set name="HTTP_ACCEPT_ENCODING" value="" />
                    </serverVariables>
                    <action type="Rewrite" url="http://localhost:1512{R0}" appendQueryString="true" logRewrittenUrl="false" />
                </rule>
                <rule name="UrlWithoutPIM" enabled="true" stopProcessing="true">
                    <match url="/*" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <serverVariables>
                        <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
                        <set name="HTTP_ACCEPT_ENCODING" value="" />
                    </serverVariables>
                    <action type="Rewrite" url="http://localhost:1512/pim/webaccess" appendQueryString="false" logRewrittenUrl="false" />
                </rule>
            </rules>
            <outboundRules>
                <clear />
                <rule name="RestoreAcceptEncoding" preCondition="NeedsRestoringAcceptEncoding">
                    <match serverVariable="HTTP_ACCEPT_ENCODING" pattern="^(.*)" />
                    <action type="Rewrite" value="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" />
                </rule>
                <preConditions>
                    <preCondition name="NeedsRestoringAcceptEncoding">
                        <add input="{HTTP_X_ORIGINAL_ACCEPT_ENCODING}" pattern=".+" />
                    </preCondition>
                </preConditions>
            </outboundRules>
        </rewrite>
        <urlCompression doStaticCompression="false" />
    </system.webServer>
</configuration>

2 个答案:

答案 0 :(得分:0)

我发现您使用/pim.作为url匹配部分。但这只会匹配pim/scripts/script.js网址格式。因此,您会发现它总是与第一个规则不匹配。

正确的规则如下:

             <rule name="UrlWithPimRewrite" stopProcessing="true">
                <match url="pim.*" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                <serverVariables>
                    <set name="HTTP_X_ORIGINAL_ACCEPT_ENCODING" value="{HTTP_ACCEPT_ENCODING}" />
                    <set name="HTTP_ACCEPT_ENCODING" value="" />
                </serverVariables>
                <action type="Rewrite" url="http://localhost:1512{R0}" appendQueryString="true" logRewrittenUrl="false" />
            </rule>

答案 1 :(得分:0)

@ brando-zhang

我更改了原始规则,现在有了以下配置。刚开始使用http://servername:123/的最初版本可以使用,但其他所有功能(例如http://servername:123/pim/scripts/script.js由于某种原因给了我404。有任何想法吗??

private void tbCUSTOMERID_MouseClick(object sender, MouseEventArgs e)
        {

            switch (e.Button)
            {
                case MouseButtons.Left:
                    break;
                case MouseButtons.Right:
                    DialogResult dialogResult = MessageBox.Show("Are you sure you want to search for this customer?", "Customer ID Search", MessageBoxButtons.YesNo);
                    if (dialogResult == DialogResult.Yes)
                    {
                        int found = Array.BinarySearch(cust, tbCUSTOMERID.Text);

                        if (found < 0)
                        {
                            MessageBox.Show("Customer doesn't exist");
                        }
                        else
                        {
                            MessageBox.Show("Customer found!");
                            tbCUSTOMERID.Text = cust[found].GScID;
                            tbCUSTOMERNAME.Text = cust[found].GSname;
                            tbCITY.Text = cust[found].GSlocation;
                            tbEMAIL.Text = cust[found].GSemail;
                        }
                    }
                    else
                    {
                    }
                    break;
            }


            /*
            if(e.Button == MouseButtons.Right)
            {
                DialogResult dialogResult = MessageBox.Show("Are you sure you want to search for this customer?", "Customer ID Search", MessageBoxButtons.YesNo);
                if (dialogResult == DialogResult.Yes)
                {
                    int found = Array.BinarySearch(cust, tbCUSTOMERID.Text);

                    if(found < 0)
                    {
                        MessageBox.Show("Customer doesn't exist");
                    }
                    else
                    {
                        MessageBox.Show("Customer found!");
                        tbCUSTOMERID.Text = cust[found].GScID;
                        tbCUSTOMERNAME.Text = cust[found].GSname;
                        tbCITY.Text = cust[found].GSlocation;
                        tbEMAIL.Text = cust[found].GSemail;
                    }
                }
                else
                {
                }
            }
            else
            {
            }
            */
        }