473,385 Members | 1,453 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How to 301 redirect default.aspx to root

How can I 301 redirect www.example.com/default.aspx to www.example.com
without using ISAPI filters?

Nov 26 '07 #1
10 8994
If i recall correctly, can't you just set this up in IIS, i just googled
"301 redirect" and got
http://www.webconfs.com/how-to-redirect-a-webpage.php as the first link...

:)
Andrew

"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ef****************@TK2MSFTNGP05.phx.gbl...
How can I 301 redirect www.example.com/default.aspx to www.example.com
without using ISAPI filters?

Nov 26 '07 #2
Yeah I now how to 301 redirect. The problem is to do it with default.aspx,
without causing an infinite loop. The homepage has 2 url's
www.eample.com/default.aspx and www.example.com. I want to 301 redirect
anyone who get default.aspx in the url.

Request.RawUrl returns default.aspx in both cases, so i can't figure out how
to check if the user has default.aspx in his url.

"Andrew Brook" <yk****@hotmail.comskrev i melding
news:%2****************@TK2MSFTNGP05.phx.gbl...
If i recall correctly, can't you just set this up in IIS, i just googled
"301 redirect" and got
http://www.webconfs.com/how-to-redirect-a-webpage.php as the first link...

:)
Andrew

"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ef****************@TK2MSFTNGP05.phx.gbl...
>How can I 301 redirect www.example.com/default.aspx to www.example.com
without using ISAPI filters?


Nov 26 '07 #3
I use request.Url.AbsoluteUri which seems to work fine unless I pass a
parameter in the query string.

-----Original Message-----
From: Eirik Eldorsen [mailto:ei***@increo.no]
Posted At: Monday, November 26, 2007 6:46 AM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to 301 redirect default.aspx to root
Subject: Re: How to 301 redirect default.aspx to root

Yeah I now how to 301 redirect. The problem is to do it with
default.aspx,
without causing an infinite loop. The homepage has 2 url's
www.eample.com/default.aspx and www.example.com. I want to 301 redirect
anyone who get default.aspx in the url.

Request.RawUrl returns default.aspx in both cases, so i can't figure out
how
to check if the user has default.aspx in his url.

"Andrew Brook" <yk****@hotmail.comskrev i melding
news:%2****************@TK2MSFTNGP05.phx.gbl...
If i recall correctly, can't you just set this up in IIS, i just
googled
"301 redirect" and got
http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
link...
>
:)
Andrew

"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ef****************@TK2MSFTNGP05.phx.gbl...
>How can I 301 redirect www.example.com/default.aspx to
www.example.com
>without using ISAPI filters?


Nov 26 '07 #4
I've tested your sugestion. I get the same problem:
If I write Response.Write(Request.Url.AbsoluteUri ) in the pageload of
Default.aspx i get http://www.example.com/deafult.aspx even if the url in
the browser says www.example.com
"Dave Bush" <da*******@dmbcllc.comskrev i melding
news:F7D602751B594FAFB02BE26C119F8393@OfficeVista. ..
>I use request.Url.AbsoluteUri which seems to work fine unless I pass a
parameter in the query string.

-----Original Message-----
From: Eirik Eldorsen [mailto:ei***@increo.no]
Posted At: Monday, November 26, 2007 6:46 AM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to 301 redirect default.aspx to root
Subject: Re: How to 301 redirect default.aspx to root

Yeah I now how to 301 redirect. The problem is to do it with
default.aspx,
without causing an infinite loop. The homepage has 2 url's
www.eample.com/default.aspx and www.example.com. I want to 301 redirect
anyone who get default.aspx in the url.

Request.RawUrl returns default.aspx in both cases, so i can't figure out
how
to check if the user has default.aspx in his url.

"Andrew Brook" <yk****@hotmail.comskrev i melding
news:%2****************@TK2MSFTNGP05.phx.gbl...
>If i recall correctly, can't you just set this up in IIS, i just
googled
>"301 redirect" and got
http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
link...
>>
:)
Andrew

"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ef****************@TK2MSFTNGP05.phx.gbl...
>>How can I 301 redirect www.example.com/default.aspx to
www.example.com
>>without using ISAPI filters?



Nov 26 '07 #5
You don't have to issue a 301...

Your request sounds like a job for Ionic's ISAPI Rewrite Filter, but you'll only be able
to use it on a server you control. Shared hosters seldom let you install ISAPI filters.

IIRF :
http://cheeso.members.winisp.net/IIRF.aspx

It's free.

To remove default.aspx from the URL, you'd use :

RewriteRule (.*)/default.aspx$ $1/ [I,RP]

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Eirik Eldorsen" <ei***@increo.nowrote in message news:ed*************@TK2MSFTNGP06.phx.gbl...
Yeah I now how to 301 redirect. The problem is to do it with default.aspx, without causing an infinite loop. The
homepage has 2 url's www.eample.com/default.aspx and www.example.com. I want to 301 redirect anyone who get
default.aspx in the url.

Request.RawUrl returns default.aspx in both cases, so i can't figure out how to check if the user has default.aspx in
his url.

"Andrew Brook" <yk****@hotmail.comskrev i melding news:%2****************@TK2MSFTNGP05.phx.gbl...
>If i recall correctly, can't you just set this up in IIS, i just googled "301 redirect" and got
http://www.webconfs.com/how-to-redirect-a-webpage.php as the first link...

:)
Andrew

"Eirik Eldorsen" <ei***@increo.nowrote in message news:ef****************@TK2MSFTNGP05.phx.gbl...
>>How can I 301 redirect www.example.com/default.aspx to www.example.com without using ISAPI filters?



Nov 26 '07 #6
Thanks. I was hoping to find a solution without using ISAPI filter, but I
guess that is the only option, or wait till IIS7 is available.

Eirik

"Juan T. Llibre" <no***********@nowhere.comskrev i melding
news:eP**************@TK2MSFTNGP06.phx.gbl...
You don't have to issue a 301...

Your request sounds like a job for Ionic's ISAPI Rewrite Filter, but
you'll only be able
to use it on a server you control. Shared hosters seldom let you install
ISAPI filters.

IIRF :
http://cheeso.members.winisp.net/IIRF.aspx

It's free.

To remove default.aspx from the URL, you'd use :

RewriteRule (.*)/default.aspx$ $1/ [I,RP]

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ed*************@TK2MSFTNGP06.phx.gbl...
>Yeah I now how to 301 redirect. The problem is to do it with
default.aspx, without causing an infinite loop. The homepage has 2 url's
www.eample.com/default.aspx and www.example.com. I want to 301 redirect
anyone who get default.aspx in the url.

Request.RawUrl returns default.aspx in both cases, so i can't figure out
how to check if the user has default.aspx in his url.

"Andrew Brook" <yk****@hotmail.comskrev i melding
news:%2****************@TK2MSFTNGP05.phx.gbl...
>>If i recall correctly, can't you just set this up in IIS, i just googled
"301 redirect" and got
http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
link...

:)
Andrew

"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ef****************@TK2MSFTNGP05.phx.gbl.. .
How can I 301 redirect www.example.com/default.aspx to www.example.com
without using ISAPI filters?





Nov 26 '07 #7
You can do this in an httpmodule

Here's one I wrote that uses a table in a database to match and replace.
It will handle 404 errors as well as being pre-emptive. It will either
do a 301 redirect or RewritePath (based on flag in db)

Table Schema:
OldUrl : varchar(n) - RegEx match or literal (based on field below)
NewUrl : varchar(n) - literal url to redirect to
Redirect301: bit - true if 301 redirect, false if RewritePath
Literal: bit - specifies that the OldUrl is a literal value and not a
pattern match.

I use this in my DotNetNuke sites regularly.

----< code start ---

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Caching;

namespace DNNRedirect
{
public class HttpModule : IHttpModule
{
public HttpModule()
{
//
// TODO: Add constructor logic here
//
}

public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(this.OnBeginRequest);
}

public void OnBeginRequest(object s, EventArgs e)
{
HttpApplication app = (HttpApplication)s;
HttpRequest request = app.Request;
HttpResponse response = app.Response;
String requestUrl = request.Url.AbsoluteUri;
String domainPath =
request.Url.AbsoluteUri.Replace(request.Url.PathAn dQuery, "");
String appPath = request.ApplicationPath;
if (appPath.Length 2)
appPath += "/";
appPath = domainPath + appPath;

RedirectorDataSet.dmbcllcRedirectDataTable redirectTable;
redirectTable = RedirectController.GetData();
int rowIndex = 0;
// Handle 404 Errors.
if (request.ServerVariables["QUERY_STRING"].Length 4 &&
request.ServerVariables["QUERY_STRING"].StartsWith("404;"))
{
requestUrl =
request.ServerVariables["QUERY_STRING"].Substring(4).ToLower();
}
// Otherwise, be pre-emptive.

string newURL = null;
bool redirect = false;
for (rowIndex = 0; rowIndex < redirectTable.Rows.Count;
rowIndex++)
{
RedirectorDataSet.dmbcllcRedirectRow row =
(RedirectorDataSet.dmbcllcRedirectRow)(redirectTab le.Rows[rowIndex]);
if (row.Literal)
{
row.OldUrl = row.OldUrl.Replace("~/", appPath);
if (row.OldUrl.ToLower() == requestUrl.ToLower())
{
newURL = row.NewUrl;
redirect = row.Redirect301;
break;
}
}
else
{
row.OldUrl = row.OldUrl.Replace("~/",
Regex.Escape(appPath));
if (Regex.IsMatch(requestUrl, row.OldUrl,
RegexOptions.IgnoreCase))
{
newURL = Regex.Replace(requestUrl, row.OldUrl,
row.NewUrl, RegexOptions.IgnoreCase);
redirect = row.Redirect301;
break;
}
}
}

if (newURL != null)
{
newURL = newURL.Replace("~/", appPath);
if (newURL.ToLower() != requestUrl.ToLower())
{
if (redirect)
{
response.Status = "301 Moved Permanently";
response.AddHeader("Location", newURL);
response.End();
return;
}
else
{
response.ClearContent();
response.StatusCode = 200;
newURL = newURL.Replace(appPath, "~/");
HttpContext.Current.RewritePath(newURL);
}
}
}
}
public void Dispose()
{
}
}

}

----< end ----

-----Original Message-----
From: Eirik Eldorsen [mailto:ei***@increo.no]
Posted At: Monday, November 26, 2007 8:28 AM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to 301 redirect default.aspx to root
Subject: Re: How to 301 redirect default.aspx to root

Thanks. I was hoping to find a solution without using ISAPI filter, but
I
guess that is the only option, or wait till IIS7 is available.

Eirik

"Juan T. Llibre" <no***********@nowhere.comskrev i melding
news:eP**************@TK2MSFTNGP06.phx.gbl...
You don't have to issue a 301...

Your request sounds like a job for Ionic's ISAPI Rewrite Filter, but
you'll only be able

to use it on a server you control. Shared hosters seldom let you install
ISAPI filters.

IIRF :
http://cheeso.members.winisp.net/IIRF.aspx

It's free.

To remove default.aspx from the URL, you'd use :

RewriteRule (.*)/default.aspx$ $1/ [I,RP]

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ed*************@TK2MSFTNGP06.phx.gbl...
>Yeah I now how to 301 redirect. The problem is to do it with
default.aspx, without causing an infinite loop. The homepage has 2
url's
>www.eample.com/default.aspx and www.example.com. I want to 301
redirect
>anyone who get default.aspx in the url.

Request.RawUrl returns default.aspx in both cases, so i can't figure
out
>how to check if the user has default.aspx in his url.

"Andrew Brook" <yk****@hotmail.comskrev i melding
news:%2****************@TK2MSFTNGP05.phx.gbl...
>>If i recall correctly, can't you just set this up in IIS, i just
googled
>>"301 redirect" and got
http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
link...

:)
Andrew

"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ef****************@TK2MSFTNGP05.phx.gbl.. .
How can I 301 redirect www.example.com/default.aspx to
www.example.com
>>>without using ISAPI filters?





Nov 26 '07 #8
am I missing something?

would a few lines like this do?
Dim url As String = Request.ServerVariables("URL")
If InStr(url, "default.aspx") <0 Then
Response.Redirect("/")
End If
"Dave Bush" <da*******@dmbcllc.comwrote in message
news:623D89CCDA0840B38E99691CFA321876@OfficeVista. ..
You can do this in an httpmodule

Here's one I wrote that uses a table in a database to match and replace.
It will handle 404 errors as well as being pre-emptive. It will either
do a 301 redirect or RewritePath (based on flag in db)

Table Schema:
OldUrl : varchar(n) - RegEx match or literal (based on field below)
NewUrl : varchar(n) - literal url to redirect to
Redirect301: bit - true if 301 redirect, false if RewritePath
Literal: bit - specifies that the OldUrl is a literal value and not a
pattern match.

I use this in my DotNetNuke sites regularly.

----< code start ---

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Caching;

namespace DNNRedirect
{
public class HttpModule : IHttpModule
{
public HttpModule()
{
//
// TODO: Add constructor logic here
//
}

public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(this.OnBeginRequest);
}

public void OnBeginRequest(object s, EventArgs e)
{
HttpApplication app = (HttpApplication)s;
HttpRequest request = app.Request;
HttpResponse response = app.Response;
String requestUrl = request.Url.AbsoluteUri;
String domainPath =
request.Url.AbsoluteUri.Replace(request.Url.PathAn dQuery, "");
String appPath = request.ApplicationPath;
if (appPath.Length 2)
appPath += "/";
appPath = domainPath + appPath;

RedirectorDataSet.dmbcllcRedirectDataTable redirectTable;
redirectTable = RedirectController.GetData();
int rowIndex = 0;
// Handle 404 Errors.
if (request.ServerVariables["QUERY_STRING"].Length 4 &&
request.ServerVariables["QUERY_STRING"].StartsWith("404;"))
{
requestUrl =
request.ServerVariables["QUERY_STRING"].Substring(4).ToLower();
}
// Otherwise, be pre-emptive.

string newURL = null;
bool redirect = false;
for (rowIndex = 0; rowIndex < redirectTable.Rows.Count;
rowIndex++)
{
RedirectorDataSet.dmbcllcRedirectRow row =
(RedirectorDataSet.dmbcllcRedirectRow)(redirectTab le.Rows[rowIndex]);
if (row.Literal)
{
row.OldUrl = row.OldUrl.Replace("~/", appPath);
if (row.OldUrl.ToLower() == requestUrl.ToLower())
{
newURL = row.NewUrl;
redirect = row.Redirect301;
break;
}
}
else
{
row.OldUrl = row.OldUrl.Replace("~/",
Regex.Escape(appPath));
if (Regex.IsMatch(requestUrl, row.OldUrl,
RegexOptions.IgnoreCase))
{
newURL = Regex.Replace(requestUrl, row.OldUrl,
row.NewUrl, RegexOptions.IgnoreCase);
redirect = row.Redirect301;
break;
}
}
}

if (newURL != null)
{
newURL = newURL.Replace("~/", appPath);
if (newURL.ToLower() != requestUrl.ToLower())
{
if (redirect)
{
response.Status = "301 Moved Permanently";
response.AddHeader("Location", newURL);
response.End();
return;
}
else
{
response.ClearContent();
response.StatusCode = 200;
newURL = newURL.Replace(appPath, "~/");
HttpContext.Current.RewritePath(newURL);
}
}
}
}
public void Dispose()
{
}
}

}

----< end ----

-----Original Message-----
From: Eirik Eldorsen [mailto:ei***@increo.no]
Posted At: Monday, November 26, 2007 8:28 AM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to 301 redirect default.aspx to root
Subject: Re: How to 301 redirect default.aspx to root

Thanks. I was hoping to find a solution without using ISAPI filter, but
I
guess that is the only option, or wait till IIS7 is available.

Eirik

"Juan T. Llibre" <no***********@nowhere.comskrev i melding
news:eP**************@TK2MSFTNGP06.phx.gbl...
>You don't have to issue a 301...

Your request sounds like a job for Ionic's ISAPI Rewrite Filter, but
you'll only be able


to use it on a server you control. Shared hosters seldom let you install
>ISAPI filters.

IIRF :
http://cheeso.members.winisp.net/IIRF.aspx

It's free.

To remove default.aspx from the URL, you'd use :

RewriteRule (.*)/default.aspx$ $1/ [I,RP]

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ed*************@TK2MSFTNGP06.phx.gbl...
>>Yeah I now how to 301 redirect. The problem is to do it with
default.aspx, without causing an infinite loop. The homepage has 2
url's
>>www.eample.com/default.aspx and www.example.com. I want to 301
redirect
>>anyone who get default.aspx in the url.

Request.RawUrl returns default.aspx in both cases, so i can't figure
out
>>how to check if the user has default.aspx in his url.

"Andrew Brook" <yk****@hotmail.comskrev i melding
news:%2****************@TK2MSFTNGP05.phx.gbl.. .
If i recall correctly, can't you just set this up in IIS, i just
googled
>>>"301 redirect" and got
http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
link...

:)
Andrew

"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ef****************@TK2MSFTNGP05.phx.gbl. ..
How can I 301 redirect www.example.com/default.aspx to
www.example.com
>>>>without using ISAPI filters?
>
>
>



Dec 3 '07 #9
Yes, that's what I initially thought, too, but ran into a wall.

Have you actually tested that ?
IIRC, when I tried somthing similar, with InStr, I ended up in an endless redirect loop.

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"ThatsIT.net.au" <me@thatsitwrote in message news:2D**********************************@microsof t.com...
am I missing something?

would a few lines like this do?
Dim url As String = Request.ServerVariables("URL")
If InStr(url, "default.aspx") <0 Then
Response.Redirect("/")
End If
"Dave Bush" <da*******@dmbcllc.comwrote in message news:623D89CCDA0840B38E99691CFA321876@OfficeVista. ..
>You can do this in an httpmodule

Here's one I wrote that uses a table in a database to match and replace.
It will handle 404 errors as well as being pre-emptive. It will either
do a 301 redirect or RewritePath (based on flag in db)

Table Schema:
OldUrl : varchar(n) - RegEx match or literal (based on field below)
NewUrl : varchar(n) - literal url to redirect to
Redirect301: bit - true if 301 redirect, false if RewritePath
Literal: bit - specifies that the OldUrl is a literal value and not a
pattern match.

I use this in my DotNetNuke sites regularly.

----< code start ---

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Caching;

namespace DNNRedirect
{
public class HttpModule : IHttpModule
{
public HttpModule()
{
//
// TODO: Add constructor logic here
//
}

public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(this.OnBeginRequest);
}

public void OnBeginRequest(object s, EventArgs e)
{
HttpApplication app = (HttpApplication)s;
HttpRequest request = app.Request;
HttpResponse response = app.Response;
String requestUrl = request.Url.AbsoluteUri;
String domainPath =
request.Url.AbsoluteUri.Replace(request.Url.PathA ndQuery, "");
String appPath = request.ApplicationPath;
if (appPath.Length 2)
appPath += "/";
appPath = domainPath + appPath;

RedirectorDataSet.dmbcllcRedirectDataTable redirectTable;
redirectTable = RedirectController.GetData();
int rowIndex = 0;
// Handle 404 Errors.
if (request.ServerVariables["QUERY_STRING"].Length 4 &&
request.ServerVariables["QUERY_STRING"].StartsWith("404;"))
{
requestUrl =
request.ServerVariables["QUERY_STRING"].Substring(4).ToLower();
}
// Otherwise, be pre-emptive.

string newURL = null;
bool redirect = false;
for (rowIndex = 0; rowIndex < redirectTable.Rows.Count;
rowIndex++)
{
RedirectorDataSet.dmbcllcRedirectRow row =
(RedirectorDataSet.dmbcllcRedirectRow)(redirectTa ble.Rows[rowIndex]);
if (row.Literal)
{
row.OldUrl = row.OldUrl.Replace("~/", appPath);
if (row.OldUrl.ToLower() == requestUrl.ToLower())
{
newURL = row.NewUrl;
redirect = row.Redirect301;
break;
}
}
else
{
row.OldUrl = row.OldUrl.Replace("~/",
Regex.Escape(appPath));
if (Regex.IsMatch(requestUrl, row.OldUrl,
RegexOptions.IgnoreCase))
{
newURL = Regex.Replace(requestUrl, row.OldUrl,
row.NewUrl, RegexOptions.IgnoreCase);
redirect = row.Redirect301;
break;
}
}
}

if (newURL != null)
{
newURL = newURL.Replace("~/", appPath);
if (newURL.ToLower() != requestUrl.ToLower())
{
if (redirect)
{
response.Status = "301 Moved Permanently";
response.AddHeader("Location", newURL);
response.End();
return;
}
else
{
response.ClearContent();
response.StatusCode = 200;
newURL = newURL.Replace(appPath, "~/");
HttpContext.Current.RewritePath(newURL);
}
}
}
}
public void Dispose()
{
}
}

}

----< end ----

-----Original Message-----
From: Eirik Eldorsen [mailto:ei***@increo.no]
Posted At: Monday, November 26, 2007 8:28 AM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to 301 redirect default.aspx to root
Subject: Re: How to 301 redirect default.aspx to root

Thanks. I was hoping to find a solution without using ISAPI filter, but
I
guess that is the only option, or wait till IIS7 is available.

Eirik

"Juan T. Llibre" <no***********@nowhere.comskrev i melding
news:eP**************@TK2MSFTNGP06.phx.gbl...
>>You don't have to issue a 301...

Your request sounds like a job for Ionic's ISAPI Rewrite Filter, but
you'll only be able


to use it on a server you control. Shared hosters seldom let you install
>>ISAPI filters.

IIRF :
http://cheeso.members.winisp.net/IIRF.aspx

It's free.

To remove default.aspx from the URL, you'd use :

RewriteRule (.*)/default.aspx$ $1/ [I,RP]

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ed*************@TK2MSFTNGP06.phx.gbl...
Yeah I now how to 301 redirect. The problem is to do it with
default.aspx, without causing an infinite loop. The homepage has 2
url's
>>>www.eample.com/default.aspx and www.example.com. I want to 301
redirect
>>>anyone who get default.aspx in the url.

Request.RawUrl returns default.aspx in both cases, so i can't figure
out
>>>how to check if the user has default.aspx in his url.

"Andrew Brook" <yk****@hotmail.comskrev i melding
news:%2****************@TK2MSFTNGP05.phx.gbl. ..
If i recall correctly, can't you just set this up in IIS, i just
googled
>>>>"301 redirect" and got
http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
link...
>
:)
Andrew
>
"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ef****************@TK2MSFTNGP05.phx.gbl.. .
>How can I 301 redirect www.example.com/default.aspx to
www.example.com
>>>>>without using ISAPI filters?
>>
>>
>>
>
>



Dec 3 '07 #10

"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:OU**************@TK2MSFTNGP03.phx.gbl...
Yes, that's what I initially thought, too, but ran into a wall.

Have you actually tested that ?
IIRC, when I tried somthing similar, with InStr, I ended up in an endless
redirect loop.

Yes I did test it it worked fine, thats why I asked if I was missing
something.
Try It again maybe you made a simple error because its working for me

>
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"ThatsIT.net.au" <me@thatsitwrote in message
news:2D**********************************@microsof t.com...
>am I missing something?

would a few lines like this do?
Dim url As String = Request.ServerVariables("URL")
If InStr(url, "default.aspx") <0 Then
Response.Redirect("/")
End If
"Dave Bush" <da*******@dmbcllc.comwrote in message
news:623D89CCDA0840B38E99691CFA321876@OfficeVista ...
>>You can do this in an httpmodule

Here's one I wrote that uses a table in a database to match and replace.
It will handle 404 errors as well as being pre-emptive. It will either
do a 301 redirect or RewritePath (based on flag in db)

Table Schema:
OldUrl : varchar(n) - RegEx match or literal (based on field below)
NewUrl : varchar(n) - literal url to redirect to
Redirect301: bit - true if 301 redirect, false if RewritePath
Literal: bit - specifies that the OldUrl is a literal value and not a
pattern match.

I use this in my DotNetNuke sites regularly.

----< code start ---

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;
using System.Web.Caching;

namespace DNNRedirect
{
public class HttpModule : IHttpModule
{
public HttpModule()
{
//
// TODO: Add constructor logic here
//
}

public void Init(HttpApplication app)
{
app.BeginRequest += new EventHandler(this.OnBeginRequest);
}

public void OnBeginRequest(object s, EventArgs e)
{
HttpApplication app = (HttpApplication)s;
HttpRequest request = app.Request;
HttpResponse response = app.Response;
String requestUrl = request.Url.AbsoluteUri;
String domainPath =
request.Url.AbsoluteUri.Replace(request.Url.Path AndQuery, "");
String appPath = request.ApplicationPath;
if (appPath.Length 2)
appPath += "/";
appPath = domainPath + appPath;

RedirectorDataSet.dmbcllcRedirectDataTable redirectTable;
redirectTable = RedirectController.GetData();
int rowIndex = 0;
// Handle 404 Errors.
if (request.ServerVariables["QUERY_STRING"].Length 4 &&
request.ServerVariables["QUERY_STRING"].StartsWith("404;"))
{
requestUrl =
request.ServerVariables["QUERY_STRING"].Substring(4).ToLower();
}
// Otherwise, be pre-emptive.

string newURL = null;
bool redirect = false;
for (rowIndex = 0; rowIndex < redirectTable.Rows.Count;
rowIndex++)
{
RedirectorDataSet.dmbcllcRedirectRow row =
(RedirectorDataSet.dmbcllcRedirectRow)(redirectT able.Rows[rowIndex]);
if (row.Literal)
{
row.OldUrl = row.OldUrl.Replace("~/", appPath);
if (row.OldUrl.ToLower() == requestUrl.ToLower())
{
newURL = row.NewUrl;
redirect = row.Redirect301;
break;
}
}
else
{
row.OldUrl = row.OldUrl.Replace("~/",
Regex.Escape(appPath));
if (Regex.IsMatch(requestUrl, row.OldUrl,
RegexOptions.IgnoreCase))
{
newURL = Regex.Replace(requestUrl, row.OldUrl,
row.NewUrl, RegexOptions.IgnoreCase);
redirect = row.Redirect301;
break;
}
}
}

if (newURL != null)
{
newURL = newURL.Replace("~/", appPath);
if (newURL.ToLower() != requestUrl.ToLower())
{
if (redirect)
{
response.Status = "301 Moved Permanently";
response.AddHeader("Location", newURL);
response.End();
return;
}
else
{
response.ClearContent();
response.StatusCode = 200;
newURL = newURL.Replace(appPath, "~/");
HttpContext.Current.RewritePath(newURL);
}
}
}
}
public void Dispose()
{
}
}

}

----< end ----

-----Original Message-----
From: Eirik Eldorsen [mailto:ei***@increo.no]
Posted At: Monday, November 26, 2007 8:28 AM
Posted To: microsoft.public.dotnet.framework.aspnet
Conversation: How to 301 redirect default.aspx to root
Subject: Re: How to 301 redirect default.aspx to root

Thanks. I was hoping to find a solution without using ISAPI filter, but
I
guess that is the only option, or wait till IIS7 is available.

Eirik

"Juan T. Llibre" <no***********@nowhere.comskrev i melding
news:eP**************@TK2MSFTNGP06.phx.gbl...
You don't have to issue a 301...

Your request sounds like a job for Ionic's ISAPI Rewrite Filter, but
you'll only be able

to use it on a server you control. Shared hosters seldom let you install

ISAPI filters.

IIRF :
http://cheeso.members.winisp.net/IIRF.aspx

It's free.

To remove default.aspx from the URL, you'd use :

RewriteRule (.*)/default.aspx$ $1/ [I,RP]

Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Eirik Eldorsen" <ei***@increo.nowrote in message
news:ed*************@TK2MSFTNGP06.phx.gbl...
Yeah I now how to 301 redirect. The problem is to do it with
default.aspx, without causing an infinite loop. The homepage has 2
url's
www.eample.com/default.aspx and www.example.com. I want to 301
redirect
anyone who get default.aspx in the url.
>
Request.RawUrl returns default.aspx in both cases, so i can't figure
out
how to check if the user has default.aspx in his url.
>
>
>
"Andrew Brook" <yk****@hotmail.comskrev i melding
news:%2****************@TK2MSFTNGP05.phx.gbl.. .
>If i recall correctly, can't you just set this up in IIS, i just
googled
>"301 redirect" and got
>http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
>link...
>>
>:)
>Andrew
>>
>"Eirik Eldorsen" <ei***@increo.nowrote in message
>news:ef****************@TK2MSFTNGP05.phx.gbl. ..
>>How can I 301 redirect www.example.com/default.aspx to
www.example.com
>>without using ISAPI filters?
>>>
>>>
>>>
>>
>>
>
>


Dec 22 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: clintonG | last post by:
Just what is the optimal model? Rather than use default.aspx to return HTML I would like to use default.aspx to return utlity functions that will determine if cookies are enabled and so on and...
7
by: Frankie | last post by:
I just acquired a new customer who had an existing ASP classic Web site. When I took over, I transferred all content to ASPX pages that have different page names than the prior Web site had. I kept...
9
by: David Veeneman | last post by:
I'm just getting started with ASP.NET, using VS 2005. As an exercise, I opened the root web site in VS 2005 and created a simple welcome page. I saved the page as Default.aspx and made sure that...
3
by: Gaetan | last post by:
I have setup 2 servers (Windows 2003 Server SP1, latest hotfix) with IIS and .Net Framework 1.1 and 2.0. None of the 2 servers have default.aspx listed as a default content document even when there...
2
by: MilanB | last post by:
Hello, I created HttpHandler to redirect root Default.aspx. But I don't know how to create path, that will redirect just web root Default.aspx. I tried following line, but it redirects all...
0
by: Joey | last post by:
I have a web app that contains several subfolders. In one of them, I have a page set up as "Default.aspx", so users can hit it just by typing the directory name in the browser address bar (example:...
4
by: Usenet User | last post by:
Platform: IIS 5 or 6, ASP .NET 1.1 I can configure default page for my virtual directory and "hide" that page from users. For example, if I have an ASPX from called MyPage.aspx, I can make it...
2
by: punjabplus | last post by:
hi, i want help for-- like responce.redirect("default.aspx?test=one"); query string . on redirect page address bar we have value like http:// default.aspx?test=one is there any way we can...
5
by: punjabplus | last post by:
hi, i want help for-- like responce.redirect("default.aspx?test=one"); query string . on redirect page address bar we have value like http:// default.aspx?test=one is there any way we can...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.