How can I set a redirect url for a virtual directory in IIS 7.0? I have installed the latest url rewrite module 2.x.
I can explain my problem with an example:
I have a website on my IIS 7.0 server, www.mysite.com. I decided to create a virtual directory "sales" under my site which is pointing to the website root directory. Now I need to create a redirect url for the vdir. The vdir is pointing to the same virtual root directory as my site root.
The big idea is that I can go to www.mysite/sales and I will automatically redirect to www.mysite.com?productid=200.
I tried to redirect with a rewrite url for vdir(not website), but I always get this error message:
Cannot add duplicate collection entry of type 'rule' with unique key
attribute 'name' set to "test".
This happens when I am pointing to the virtual vdir and try to add a rule.
I can add rules to the website level, but rules don't work. I mean url "www.mysite/sales" gives me the following error. I know that key is unique. I checked it from web.config.
This kind of feature was really easy use in IIS 6.0, just point at vdir with your mouse and set properties-->a redirect to url.
Please some one explain what is the right way to do it in IIS 7.0?
In IIS Manager, expand the server, expand Sites, and expand Default Web Site. Select the virtual directory, and verify Features View is selected at the bottom of the page. In the IIS section, double-click HTTP Redirect.
Actually not quite as easy, but not too horrible.
1) Create a physical directory instead of a virtual directory.
2) Make sure you have HTTP Redirect role services. See http://www.iis.net/ConfigReference/system.webServer/httpRedirect
3) Open IIS Manager and browse to the physical directory you want to redirect.
4) Double click on the 'HTTP Redirect' icon under IIS.
5) Check the "Redirect requests to this destination" box and enter the new URL (http://example.com/newPage.html)
6) Check the next two boxes as needed; I find it usually necessary to check both.
7) Press Apply button.
8) Select Web site and 'Restart' under Manage Web Site.
9) Test.
The reason this is happening is because by default all rule configurations get inherited from the parent web applications. So if there is a Rule defined inside the root website, it gets inherited on all subsequent child websites in the hierarchy; unless it is cleared.
So for Example, if the IIS application hierarchy is as follows -
Default Web Site(Root)
| Web Application 1(VDir1) | Web Sub Application 11(VDir2)
| Web Application 2(VDir3)
The Rule with a name 'Test' if exists in the Root application, it gets inherited to Web Application 1, Web Application 11 and Web Application 2
In your case Web Application 11 (VDir2) is physically at the same Location as (VDir1). Hence, the IIS configurations are the same for both the directories since it shares the same Web.config file.
So any new Rule added to the Web.config File will automatically be applied to both these Virtual Directories (VDir1 & VDir2). And Since Rules get inherited by default, the rule engine in IIS will find it a problem while parsing the rule, since it already exists by inheritance.
A possible solution would be to add a < Clear /> tag before adding any Rule inside the web.config file, so that it clears any inherited rule.
<rewrite>
<rules>
<clear/>
<rule name="test" stopProcessing="true">
<match xxx />
<action xxx />
</rule>
</rules>
</rewrite>
However, I would prefer the < Remove name='Rule Name' /> tag instead to target removing only the rule that need to be added in the inherited config. This would ensure that all the other Rules at the root website is inherited in the hierarchy; with only this new rule being (sort of) overridden.
<rewrite>
<rules>
<remove name="test"/>
<rule name="test" stopProcessing="true">
<match xxx />
<action xxx />
</rule>
</rules>
</rewrite>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With