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.
-
Not that I know of. Why would you need that? – Remy Sep 23 '10 at 10:43
-
3@Remy: Late response, but it increases readability of the web.config when you have a lot of rewrite rules. That's the reason for me anyway. – magnattic Oct 25 '13 at 13:28
-
in IIS >= 7.5 remember to install URL Rewrite component from here https://www.iis.net/downloads/microsoft/url-rewrite – Riccardo Bassilichi Feb 20 '18 at 18:02
2 Answers
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

- 1
- 1

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

- 1
- 1

- 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