Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to perform 301 redirects through asp.net 4 / IIS7

Status
Not open for further replies.

jondow

Programmer
Oct 19, 2006
45
0
0
GB
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top