28

I'm using IIS7 UrlRewrite module. I set up my rules in the web.config <system.webServer><rewrite> section. I want to know if there's a way to define the rules in one external xml file instead of in web.config file. Thanks.

Paul Tyng
  • 7,924
  • 1
  • 33
  • 57
opaera
  • 761
  • 2
  • 8
  • 8

2 Answers2

54

Yes, you can use the configSource attribute to point to an external file like you can with other web.config sections. In the web.config:

<rewrite>
    <rules configSource="Rewrite.config" />
</rewrite>

And in the rules config file:

<rules>
    <rule name="some rule">
        <!-- rule details here --->
    </rule>
</rules>

You can still even use the IIS manager to edit rules and it'll just work. One minor caveat with this approach: when you make a change and save an external file like this, it will not recycle the application like making a change to the web.config will. So if you're editing a rule and want to see it take effect, you need to manually poke the web.config by making an edit and saving it.

Another reference: Moving IIS7 url rewrite section out of the web.config file

Community
  • 1
  • 1
Kurt Schindler
  • 21,037
  • 4
  • 43
  • 48
  • Are you sure about the application not being recycled on external changes? There is another property on config sections called [RestartOnExternalChanges](http://msdn.microsoft.com/en-us/library/system.configuration.sectioninformation.restartonexternalchanges.aspx) and it defaults to true. – magnattic Oct 25 '13 at 13:26
  • 1
    @atticae I believe RestartOnExternalChanges applies only to custom defined
    elements, which rewrite rules are not. http://forums.iis.net/t/1155629.aspx I'm not sure if anything has changed since then, this thread is from 2009...
    – Kurt Schindler Oct 25 '13 at 13:53
  • Unfortunately the IntelliSense in Rewrite.config does not work. Is there any way to solve this issue? – Valerio Gentile Sep 11 '15 at 04:46
1

You can use the sample URL Rewrite providers that include one for storing those in a separate file, see: http://www.iis.net/learn/extensions/url-rewrite-module/using-custom-rewrite-providers-with-url-rewrite-module

Community
  • 1
  • 1
Carlos Aguilar Mares
  • 13,411
  • 2
  • 39
  • 36
  • but that is for mappings. Config is used for another purpose, to set custom rules. sometimes we need to use a reg expression to all the url mappings we need then the only way is to include it in config. – Blue Clouds May 04 '16 at 07:29