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 do I best achieve permanent 301 redirects with IIS7/.NET 4

Status
Not open for further replies.

jondow

Programmer
Oct 19, 2006
45
0
0
GB
Hello all,

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 think this would be better handled by IIS and/or DNS than asp.net. I would try forum41 for help on setting up the redirects.

with IIS handling the redirects you don't have to worry about this in the code.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Hi Jason,

Thanks for the reply, I thought I may be able to do this with HTTP Redirect in IIS7 but when I set "redirect requests to this destination" I got a page not found error on the site.

I'll try re-posting there and see what suggestions I get.

Rick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top