Hi All,
I asked the below question in the asp.net forum but its been suggested this could be handled through IIS rather than through code. Could anyone advise please?
I have a number of domain names pointing at the same website, for example:
Which is effectivly duplicate content from a search engine perspective, so what I want to do is 301 redirect all traffic to so a request to would get a redirect to
I have seen a number of potential solutions but cant seem to find any offical solutions (from msdn or similar) so Im wondering what is the best approach?
Here are 2 examples I have seen:
Use global.asax
<code>
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext ctx = ((HttpApplication) sender).Context;
IHttpHandler handler = ctx.Handler;
domain
if (handler is Page)
{
if (ctx.Request.Url.Host != " {
UriBuilder uri = new UriBuilder(ctx.Request.Url);
uri.Host = "
ctx.Response.AddHeader("Location", uri.ToString());
ctx.Response.StatusCode = 301;
ctx.Response.StatusDescription = "Moved Permanently";
ctx.Response.End();
}
}
}
</code>
Use web config rules
<code>
<rule name="Add >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^ />
</conditions>
<action type="Redirect" url=" appendQueryString="true" redirectType="Permanent" />
</rule>
</code>
With the first, is this a safe approach? Would the code be better placed in OnPreRequestHandlerExecute?
With the second, are there any server side requirments for this to work?
Are these good solutions? Is there a better one?
Its a difficult thing to test so I'm trying to get it right first time without doing any harm to my incoming traffic so any thoughts or suggestions would be appricated!
Thanks
Rick
I asked the below question in the asp.net forum but its been suggested this could be handled through IIS rather than through code. Could anyone advise please?
I have a number of domain names pointing at the same website, for example:
Which is effectivly duplicate content from a search engine perspective, so what I want to do is 301 redirect all traffic to so a request to would get a redirect to
I have seen a number of potential solutions but cant seem to find any offical solutions (from msdn or similar) so Im wondering what is the best approach?
Here are 2 examples I have seen:
Use global.asax
<code>
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext ctx = ((HttpApplication) sender).Context;
IHttpHandler handler = ctx.Handler;
domain
if (handler is Page)
{
if (ctx.Request.Url.Host != " {
UriBuilder uri = new UriBuilder(ctx.Request.Url);
uri.Host = "
ctx.Response.AddHeader("Location", uri.ToString());
ctx.Response.StatusCode = 301;
ctx.Response.StatusDescription = "Moved Permanently";
ctx.Response.End();
}
}
}
</code>
Use web config rules
<code>
<rule name="Add >
<match url="(.*)" ignoreCase="true" />
<conditions>
<add input="{HTTP_HOST}" negate="true" pattern="^ />
</conditions>
<action type="Redirect" url=" appendQueryString="true" redirectType="Permanent" />
</rule>
</code>
With the first, is this a safe approach? Would the code be better placed in OnPreRequestHandlerExecute?
With the second, are there any server side requirments for this to work?
Are these good solutions? Is there a better one?
Its a difficult thing to test so I'm trying to get it right first time without doing any harm to my incoming traffic so any thoughts or suggestions would be appricated!
Thanks
Rick