IIS URL rewrite rule redirect with mask?

做~自己de王妃 提交于 2019-12-11 03:33:36

问题


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>


来源:https://stackoverflow.com/questions/10678956/iis-url-rewrite-rule-redirect-with-mask

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