IIS URL重写规则重定向与掩码?

时间:2012-05-21 02:43:55

标签: iis url-rewriting rewrite

我有这样的网址:

http://www.example.com/erf4d

(其中erf4d可以是任何5个字符的字符串)

我想将用户重定向到:

http://www.example.com/viewentry.aspx?ID=erf4d

我已经使用web.config中的重写规则完成了这个,但是我希望如此 http://www.example.com/erf4d停留在URL栏中,以便用户看不到“丑陋”的viewentry.aspx?ID = erf4d是一种重定向掩码,如果你愿意的话。无论如何要实现这个目标吗?

1 个答案:

答案 0 :(得分:3)

根据我的经验,URL重写以相反的方式使用。所以页面实际上是viewentry.aspx?ID = 123,重定向会将其重定向到/ 123.

我认为你可能会以错误的方式做到这一点?如果您使用IIS 7中的向导为SEO友好URL设置URL重写并使用viewentry.aspx?ID = erf4d作为基础,它应该可以帮助您到达所需的位置。

重定向/重写工作,所以如果你转到test.com/123它会工作,但如果你转到test.com/view.aspx?ID=123,它会发送给test.com/123 。我认为你追求的是什么?

干杯

编辑:这是我使用的一个例子。它将读取news.aspx?page = 1并重写为news / 1 /。但是由于规则的原因,新闻/ 1 /实际上也有效,所以如果需要,可以在锚点中引用。

            <rule name="Redirect - /news.aspx?page=a to /news/a/" enabled="true" stopProcessing="true">
                <match url="^news\.aspx$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                    <add input="{QUERY_STRING}" pattern="^page=([0-9]{1,3})$" />
                </conditions>
                <action type="Redirect" url="news/{C:1}" appendQueryString="false" />
            </rule>
            <rule name="Rewrite - /news/a/ to page=a" enabled="true" stopProcessing="true">
                <match url="^news/([^/]+)/?$" />
                <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="news.aspx?page={R:1}" />
            </rule>