简体   繁体   中英

How do I setup a rule in web.config to rewrite to a subdirectory/file in IIS7?

I am having an issue trying to get the rewrite working in IIS7 web.config.

I need URLs like /err/interaccess to be rewritten to /err/404new.asp (not a redirect,as I don't want to expose the file.

I tried the following and it only works if I use /interaccess but not /err/interaccess

           <rule name="Rewrite Interaccess Error" enabled="true" stopProcessing="true">
                <match url="^tinteraccess$" />
                <action type="Rewrite" url="/err/404new.asp" />
            </rule>

Any idea on why? I tried to find documentation on this and could not find anything regarding this usage.

The magic is in the regular expression of the <match> tag. To make it match your exact URL you would use:

       <rule name="Rewrite Interaccess Error" enabled="true" stopProcessing="true">
            <match url="^err/interaccess$" />
            <action type="Rewrite" url="/err/404new.asp" />
        </rule>

If you would want to match everything under /err/ you would use:

      <rule name="Rewrite Interaccess Error" enabled="true" stopProcessing="true">
            <match url="^err/" />
            <action type="Rewrite" url="/err/404new.asp" />
        </rule>

This is all very well documented, eg: http://www.iis.net/downloads/microsoft/url-rewrite (see Related Learning)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM