Forum Moderators: open

Message Too Old, No Replies

Redirecting non-www request to www using Asp.Net

Is this the usual approach?

         

fischermx

1:44 am on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I want to redirect any incoming request in my site that calls a page that is not using the www prefix to the one with the prefix.

I've just come with this solution :


protected void Application_BeginRequest(Object sender, EventArgs e)
{
string serverName = Request.ServerVariables["SERVER_NAME"];
string destURL = Request.ServerVariables["URL"];

if (serverName.IndexOf("example.com") == 0)
{
Response.StatusCode = 301;
Response.RedirectLocation = "http://www.example.com"+destURL;
}
}

Using .Net/IIS, is this the "correct" way to do it?
Any other idea?

[edited by: Xoc at 7:48 am (utc) on May 26, 2005]

fischermx

10:48 pm on May 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No one else have tried this!?

LeoXIV

10:58 pm on May 18, 2005 (gmt 0)

10+ Year Member



by default[which is modifable] aspnet_isapi.dll only handles .net related requests (.aspx, ...). so if you want to include every extenstion, you need to modify that.

aspdaddy

8:36 am on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I dont get it.

Isnt it just gonna use a lot of resources and mess up your log analysis.

1) Whats the benefits?
2) There must be better way, some re-write feature in the server or firewall?

Xoc

6:55 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It is precisely to keep his log files analysis correct that he should do it. I prefer that there be a canonical url for each of my web pages. If [example.com...] has the same content as [example.com,...] then there isn't a canonical url for this content on the web. Plus, it makes sure than everyone who links to him uses the same canonical url.

In any case, there is a way to do this with pure IIS. See posting #9 in this thread: [webmasterworld.com...]