This should be a bit more flexible:
Code:using System;
using System.Web;
using System.Configuration;
using System.Text.RegularExpressions;
public class BaseUrlRedirectionModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += delegate
{
var redirectFromBaseUrl = ConfigurationManager.AppSettings["RedirectFromBaseUrl"];
if (!string.IsNullOrEmpty(redirectFromBaseUrl))
{
var pattern = '^' + Regex.Escape(redirectFromBaseUrl).Replace("\\*", ".*").Replace("\\?", ".");
var oldUrl = application.Context.Request.Url.AbsoluteUri;
var match = Regex.Match(oldUrl, pattern, RegexOptions.IgnoreCase);
if (match.Success)
{
var newUrl = ConfigurationManager.AppSettings["RedirectToBaseUrl"] + oldUrl.Substring(match.Length);
if (!string.Equals(newUrl, oldUrl, StringComparison.InvariantCultureIgnoreCase))
application.Context.Response.Redirect(newUrl);
}
}
};
}
public void Dispose() { }
}
So you define a base URL that you redirect _from_ ... this can contain wildcards. But it really should include enough of the trailing path to "match" correctly. Meaning "http://localhost*" is not good, but "http://local*/" is good. Notice the trailing slash. Without it we don't really know where the base URL ends.
Then you define a URL to redirect _to_. This doesn't contain wildcards. The portion that was "matched" from the redirect _from_ will be replaced by this value.
So for example these are the appSettings I added:
Code: <add key="RedirectFromBaseUrl" value="http://*/" />
<add key="RedirectToBaseUrl" value="http://munich.elsitech.local:8040/" />
You could add something like this:
Code: <add key="RedirectFromBaseUrl" value="http://*/" />
<add key="RedirectToBaseUrl" value="https://ssl.mysecuresite.com:8443/" />
And this can be applied per page. So rather than putting in your main <appSettings>, you can define a location in your web.config:
Code:<configuration>
<location path="Host.aspx">
<appSettings>
<add key="RedirectFromBaseUrl" value="http://*:8040/" />
<add key="RedirectToBaseUrl" value="http://munich.elsitech.local:8040/" />
</appSettings>
</location>
<system.web>