IIS URL rewrite rule redirect with mask?

2019-07-17 07:31发布

问题:

I have a URL like this:

http://www.example.com/erf4d

(where erf4d can be any 5 character string)

I want to redirect the user to:

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

I have accomplished this using rewrite rules in web.config however I would like it so that http://www.example.com/erf4d stays in the URL bar such that the user dosen't see the "ugly" viewentry.aspx?ID=erf4d a sort of redirect mask if you will. Is there anyway to accomplish this?

回答1:

In my experience URL rewrite is used the other way around. So the page is actually viewentry.aspx?ID=123 and the redirect will redirect it to /123.

I think you may be doing it the wrong way around? If you setup a URL Rewrite using the Wizard in IIS 7 for the SEO friendly URLs and use viewentry.aspx?ID=erf4d as a base, it should help get you where you need.

The redirect / rewrites work, so that if you goto test.com/123 it will work, but if you goto test.com/view.aspx?ID=123, it'll send you to test.com/123. Which is what I think you're after?

Cheers

Edit: Here's an example of something I use. It will read news.aspx?page=1 and rewrite to news/1/. But due to the rules, news/1/ actually works as well, so can be refered that way in anchors if need be.

            <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>