Multiple external files for url rewrite under web.config - asp.net

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

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.

Rewrite Maps in IIS Make web.config Too Large

I am migrating a website which is/will be running on IIS and I will be using rewrite maps to 301 redirect old ".asp" URLs to a new style of URL. For many thousands of URLs there is no pattern, so it appears I must rely on rewrite maps.
My problem is that the default web.config size limit is 250kb, and in my environment, I don't have access to change this (as can be done at the registry level - if one had access).
I have looked into moving the rewriteMaps section to an external file, but the external files also have the default size limit of 250kg, so this is also not going to work.
I am looking for some other way to handle this... I am sitting at 242kb currently and have over twice the amount of old to new redirect mapping to add.
Thanks in advance.
As I am in a shared environment, there was no solution other than to move it to a single external config file, which was again, limited to 250KB.
So here is what I did:
I filled up the map to capicity with the highest-trafficked redirects and of course any groups of urls that could be redirected using a pattern (so they would be fastest).
For the 100k remaining long-tail of the redirects, I put them into a REDIRECTS table in the db... SO, after the request passes all of the rewrite rules in the file (and of course, doesn't hit any), it defaults the request to a certain script. One of the first things the script does is check the REDIRECTS table, and if an entry exists, I do a redirect in the code... it's slower, but most of the stuff in the table is long tail, and as I said, the most visited redirects are still in the file. This has worked well for me so far, and I can add as many redirects as I want... e.g. if a page title/url gets edited, my admin area automatically adds the redirect, etc.
The Nativerd.dll file uses the value of this registry key to determine the maximum allowed size, in KB, of the Web.config files. The Configuration system assumes a default value of 250 KB in Windows Server 2008 and 100 KB in the release version of Windows Vista.
The reason for the 250KB limit is to reduce attacks for uploading a large web.config file. You can change the the limit by altering the upper value in your registry:
HKLM\SOFTWARE\Wow6432Node\Microsoft\InetStp\Configuration\MaxWebConfigFileSizeInKB (REG_DWORD)
See: Description of the registry keys that are used by IIS 7.0, IIS 7.5, and IIS 8.0
Another option is to split your web.config files into multiple smaller files.
We were stuck on this for a long while, we ended up writing our own 301 redirector. In sitecore it was in a pipeline patched after ItemResolver which consumes the large file (not included in any Web.configs). We could not use the "registry hack" option as it an Azure Service App and there is no (easy and cheap) access to the registry.
You can split up your configuration into a few different files as Neill said.
You will have a main web.config file in which you’ll reference sub config files by adding a configSource attribute onto the sections you would like to split into other files.
For example, if you’d like to split the section “appsettings” in another file, you would change the appSettings section in your web.config file to :
<appSettings configSource="appsettings.config" />
and in the appsettings.config file, you would add all your appsettings entries like they were in the original web.config file, for example;
<appSettings>
<add key="aspnet:RestrictXmlControls" value="true" />
<add key="FeedCacheTime" value="300" />
<add key="FeedPageUrl" value="/_layouts/15/feed.aspx?" />
<add key="FeedXsl1" value="/Style Library/Xsl Style Sheets/Rss.xsl" />
<add key="aspnet:AllowAnonymousImpersonation" value="true" />
</appSettings>
Obviously with the rewrite maps instead.

urlrewritingnet - rewrite URL with subdomain

I have umbraco website setting on an IIS 7 server: WWW.SITE.COM
I would like rewrite the URL WWW.SITE.COM/SIGUNP to WWW.SIGNUP.SITE.COM
is it possible by using urlrewritingnet or should I configure this by using DNS Host?
I would use HTTP Redirect in IIS to achieve this. I'd recommend using permanent (301) as response status, this will also force most search robots to update their indexes.
Add everything inside the configuration elements to your web.config file
<?xml version="1.0"?>
<configuration>
<location path="signup">
<system.webServer>
<httpRedirect enabled="true" destination="http://www.signup.site.com" exactDestination="false" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>
exactDestination="false" will turn http://www.site.com/signup/anything/example.html into http://www.signup.site.com/anything/example.html
exactDestination="true" will turn http://www.site.com/signup/anything/example.html into http://www.signup.site.com/
There are several approaches to doing this and Eric's approach is a perfectly valid one. You can also use UrlRewriting.Net as you suggest but I think Eric has suggested the baked in <httpRedirect /> approach as it can be configured in the web.config and therefore also in IIS7 manually.
The disadvantages to this approach however are that:
It needs a developer to update the web.config; or
Someone with access to IIS to change the configuration
It requires an application restart to pick up the redirect rules
There are two other approaches you should consider:
A HttpModule that uses a CSV file containing a cached list of 'from' and 'to' URLs;
A Umbraco-managed doc type that can handle specific path redirects.
The HttpModule approach obviously requires a little coding but is very rewarding. Your SEO team/client can provide a list of URLs that need redirecting and your HttpModule can cache the list (using the file as a dependency) and perform the redirects based upon matched URLs. Any update to the file simply clears the cache automatically.
For basic redirects, I like the approach of having a "Redirect" doc type in Umbraco. This doc type will have two fields, a "redirect type" field (301/302) and a "redirect to" field. In the template for this doc type you will need a little cpde that performs a redirect to the "redirect to" node. Any hits on a page created using this doc type will automatically redirect to the target page. You can also use this doc type in conjunction with "umbracoUrlAlias" field. You can add multiple paths to this field separated by a comma (see this article for an explaination). This way you can catch multiple simliar paths and redirect to a single path.
The advantage of this approach is that it is manageable in the CMS, but the disadvantage is that the redirects are not managed centrally like a CSV file, so you need to be careful in how it is implemented.

How to create website specific configuration files while running multiple ASP.NET websites on a single code base?

I want to create 2 websites on a single code base in asp.net.
lets for say :
www.domain1.com
www.domain2.com
I need to keep all the appsetting key name sames but with different values for each of the websites.So i need to maintain two web.config files in my code base.
After exploring on internet , i found to keep both of the web.config files in different folders in the code base.
Bow the problem is, how can i link each of the different web.config file with its corrosponding Virtual directory for each the website because both of the Virtual directories will target the same folder of code base.
I really neede it as i have to finish this work within this week.
Thanks
If you really want to serve both site from one application/virtual directory, the only solution I can come up with is as follows:
<appSettings>
<add key="setting[www.domain1.com]" value="foo" />
<add key="setting[www.domain2.com]" value="bar" />
...
And then access these accordingly:
var setting = ConfigurationManager.AppSettings["setting[" + request.Domain + "]"];

IIS7 urlrewrite module - Rules in external xml file

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

Resources