IIS7 urlrewrite module - Rules in external xml file - iis-7

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.

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

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

Related

How do I reload asp.net configuration file cache?

I have an asp.net application that sets the configSource attribute on the rewriteRules element in web.config to point to a separate config file:
<rewrite>
<rules configSource="App_Data\Config\RewriteRules.config" />
</rewrite>
My web app makes edits to the RewriteRules.config file programmatically, but my web app does not pick up the configuration changes after the file is edited and saved.
I have tried calling HttpRuntime.UnloadAppDomain() after editing the file. This successfully restarts my app domain, but the changes in RewriteRules.config are still not picked up. I have tried adding RestartOnExternalChanges="true" to the rewrite element, but this is apparently not supported on the IIS rewrite module. I have also tried ConfigurationManager.RefreshSection("rewrite/rules") but this does not seem to have any effect. The only way I can get the changes to take effect is to edit and save the main web.config file, but I am trying to avoid doing this programmatically for security reasons.
I am confused as to why HttpRuntime.UnloadAppDomain() does not cause external config files to be re-read. Is this expected behavior? Does the config file cache somehow exist outside the bounds of the app domain? Is there any practical way to achieve what I am looking to do?
Dude, the problem with your case is, related configSection definition is not marked as restartOnExternalChanges="true" in definition. For example; we created a custom config section for storing application urls in an external file and we create a section definition in web.config file like
<section name="pageUrlFormats" type="Kahia.Web.Configuration.PageUrlFormats.PageUrlFormatsSection, Kahia.Web" restartOnExternalChanges="true" requirePermission="false" />
so that asp.net knows if any change occurs in related file:
<pageUrlFormats configSource="Config\PageUrlFormats.config" />
application domain restarts. This goes same for all config section definitions, including UrlRewrite module's definition.
What you have to do is, find definition of related module. In this scenario, it is at apphost.config at C:\Windows\system32\inetsrv\config\applicationHost.config
In that file, look for rule section definition, it starts like
<section name="rules"
You have to add restartOnExternalChanges="true" attribute to that config file.
IIS7 configuration system uses the same syntax as the .Net framework configuration system, but is a different implementation that has some behavior differences. The restartOnExternalChanges thing is a feature of the .Net framework configuration system that is not supported by the IIS7 configuration system. The url rewriter module uses the IIS7 configuration system.

Configure ASP.NET Routing in Web.config

Is there a way to configure ASP.NET routing in the web.config file? I do not need anything fancy. If a user visits /myApp/list, I want to load /myApp/list.html. However, I do NOT want the address bar to change.
Is this possible? If so, how?
The best way is to use UrlRewrite module in IIS: http://www.iis.net/learn/extensions/url-rewrite-module
How you make this rule into web.config after installing UrlRewrite:
<system.webServer>
<rewrite>
<rules>
<rule name="my-first-url-rule" stopProcessing="true">
<match url="^/myApp/list$" />
<action type="Rewrite" url="/myApp/list.html" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
As of now there is No out-of-the-box way to configure routes in web.config file.
It seems vision was/is to add new routes on Application Start.
This doesn't stop us from creating custom configuration section and reading routes from there.
These are guesses why.
Most of the time, it seems, adding new routes could be prone to introduction of routing bugs and should be followed up with good regression testing. And therefore it probably will be done as part of new product release anyway. Perhaps that could be one the reasons why it's not configurable through configuration file.
Another reason could be that routes rules can be quite complex to put them into xml format - constraints, custom route handlers,etc.

Multiple external files for url rewrite under web.config

Earlier I wanted to keep the redirects out of web.config, because I have many rewrite rules. I was successfull in doing that by using
<rewrite>
<rules configSource="RewriteRules.config" />
</rewrite>
Now the problem is this file is already having many rules, and I am going to use another set of large amount of rules for another domain pointed to same base code. So I want to keep rules for both files on different files like:-
<rewrite>
<rules configSource="RewriteRules_a.config" />
<rules configSource="RewriteRules_b.config" />
</rewrite>
which is not allowed, can anyone suggest how can i fix this situation??
Please note both domains will have different rules and domain addition can be more too in this project.
Any suggestions will be appreciated ...
Can you use create multiple files (for development only) and create post-build rules that concatenate them all into the single rewrite.config file?
This way you can split them into multiple files and version control them during development and the post-build will aggregate them into one file for production purposes.
Does the second domain point to the same application in IIS? If they are different applications you could use the config transformation capabilities of VS to have a single file per domain and only deploy the file you need on each application:
http://www.hanselman.com/blog/SlowCheetahWebconfigTransformationSyntaxNowGeneralizedForAnyXMLConfigurationFile.aspx

IIS7 rewrite module: change rules without reset an application

I use IIS7 rewrite module and all rules are places in a separate config file (not in web.config):
<rewrite>
<rules configSource="Config\Rewrite.config" />
</rewrite>
Is it possible to changes these rules without reset related application?

ASP.net simple rewrite rule

I'm trying to rewrite non www to www domains.
I've tried the rule:
<rewrite url="http://domain\.com(.+)" to="http://www.domain.com$1" />
But to no avail. It just continues to allow access to htttp://domain.com
Most likely you are refering to this one ("Intelligencia URL Rewriter").
As described in their documentation, you have to add the configuration section handler as well as other configuration settings to your web.config file before you can start adding rewrite/redirect rules.
Update
Just saw you modified your question, so probably you managed to find the configuration issue.
For your domain issue, I handled something similar in one of my projects like this:
<!-- Ensure that all are on the same top domain and sub domain. -->
<unless header="HTTP_HOST" match="www.zeta-producer.com">
<redirect
url="^(.*)$"
to="http://www.zeta-producer.com$1"
processing="stop" />
</unless>

Resources