Connecting Tech Pros Worldwide Forums | Help | Site Map

How to 301 redirect default.aspx to root

Eirik Eldorsen
Guest
 
Posts: n/a
#1: Nov 26 '07
How can I 301 redirect www.example.com/default.aspx to www.example.com
without using ISAPI filters?




Andrew Brook
Guest
 
Posts: n/a
#2: Nov 26 '07

re: How to 301 redirect default.aspx to root


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" <eirik@increo.nowrote in message
news:ef1m%23WBMIHA.4272@TK2MSFTNGP05.phx.gbl...
Quote:
How can I 301 redirect www.example.com/default.aspx to www.example.com
without using ISAPI filters?
>
>
>

Eirik Eldorsen
Guest
 
Posts: n/a
#3: Nov 26 '07

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" <ykoorb@hotmail.comskrev i melding
news:%232pj3eBMIHA.3940@TK2MSFTNGP05.phx.gbl...
Quote:
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" <eirik@increo.nowrote in message
news:ef1m%23WBMIHA.4272@TK2MSFTNGP05.phx.gbl...
Quote:
>How can I 301 redirect www.example.com/default.aspx to www.example.com
>without using ISAPI filters?
>>
>>
>>
>
>

Dave Bush
Guest
 
Posts: n/a
#4: Nov 26 '07

re: How to 301 redirect default.aspx to root


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:eirik@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" <ykoorb@hotmail.comskrev i melding
news:%232pj3eBMIHA.3940@TK2MSFTNGP05.phx.gbl...
Quote:
If i recall correctly, can't you just set this up in IIS, i just
googled
Quote:
"301 redirect" and got
http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
link...
Quote:
>
:)
Andrew
>
"Eirik Eldorsen" <eirik@increo.nowrote in message
news:ef1m%23WBMIHA.4272@TK2MSFTNGP05.phx.gbl...
Quote:
>How can I 301 redirect www.example.com/default.aspx to
www.example.com
Quote:
Quote:
>without using ISAPI filters?
>>
>>
>>
>
>

Eirik Eldorsen
Guest
 
Posts: n/a
#5: Nov 26 '07

re: How to 301 redirect default.aspx to root


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" <davembush@dmbcllc.comskrev i melding
news:F7D602751B594FAFB02BE26C119F8393@OfficeVista. ..
Quote:
>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:eirik@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" <ykoorb@hotmail.comskrev i melding
news:%232pj3eBMIHA.3940@TK2MSFTNGP05.phx.gbl...
Quote:
>If i recall correctly, can't you just set this up in IIS, i just
googled
Quote:
>"301 redirect" and got
>http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
link...
Quote:
>>
>:)
>Andrew
>>
>"Eirik Eldorsen" <eirik@increo.nowrote in message
>news:ef1m%23WBMIHA.4272@TK2MSFTNGP05.phx.gbl...
Quote:
>>How can I 301 redirect www.example.com/default.aspx to
www.example.com
Quote:
Quote:
>>without using ISAPI filters?
>>>
>>>
>>>
>>
>>
>
>

Juan T. Llibre
Guest
 
Posts: n/a
#6: Nov 26 '07

re: How to 301 redirect default.aspx to root


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" <eirik@increo.nowrote in message news:edCBwHCMIHA.820@TK2MSFTNGP06.phx.gbl...
Quote:
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" <ykoorb@hotmail.comskrev i melding news:%232pj3eBMIHA.3940@TK2MSFTNGP05.phx.gbl...
Quote:
>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" <eirik@increo.nowrote in message news:ef1m%23WBMIHA.4272@TK2MSFTNGP05.phx.gbl...
Quote:
>>How can I 301 redirect www.example.com/default.aspx to www.example.com without using ISAPI filters?
>>>
>>>
>>>
>>
>>
>
>

Eirik Eldorsen
Guest
 
Posts: n/a
#7: Nov 26 '07

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" <nomailreplies@nowhere.comskrev i melding
news:ePYWQ8CMIHA.3852@TK2MSFTNGP06.phx.gbl...
Quote:
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" <eirik@increo.nowrote in message
news:edCBwHCMIHA.820@TK2MSFTNGP06.phx.gbl...
Quote:
>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" <ykoorb@hotmail.comskrev i melding
>news:%232pj3eBMIHA.3940@TK2MSFTNGP05.phx.gbl...
Quote:
>>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" <eirik@increo.nowrote in message
>>news:ef1m%23WBMIHA.4272@TK2MSFTNGP05.phx.gbl.. .
>>>How can I 301 redirect www.example.com/default.aspx to www.example.com
>>>without using ISAPI filters?
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>

Dave Bush
Guest
 
Posts: n/a
#8: Nov 26 '07

re: How to 301 redirect default.aspx to root


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:eirik@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" <nomailreplies@nowhere.comskrev i melding
news:ePYWQ8CMIHA.3852@TK2MSFTNGP06.phx.gbl...
Quote:
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
Quote:
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" <eirik@increo.nowrote in message
news:edCBwHCMIHA.820@TK2MSFTNGP06.phx.gbl...
Quote:
>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
Quote:
Quote:
>www.eample.com/default.aspx and www.example.com. I want to 301
redirect
Quote:
Quote:
>anyone who get default.aspx in the url.
>>
>Request.RawUrl returns default.aspx in both cases, so i can't figure
out
Quote:
Quote:
>how to check if the user has default.aspx in his url.
>>
>>
>>
>"Andrew Brook" <ykoorb@hotmail.comskrev i melding
>news:%232pj3eBMIHA.3940@TK2MSFTNGP05.phx.gbl...
Quote:
>>If i recall correctly, can't you just set this up in IIS, i just
googled
Quote:
Quote:
Quote:
>>"301 redirect" and got
>>http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
>>link...
>>>
>>:)
>>Andrew
>>>
>>"Eirik Eldorsen" <eirik@increo.nowrote in message
>>news:ef1m%23WBMIHA.4272@TK2MSFTNGP05.phx.gbl.. .
>>>How can I 301 redirect www.example.com/default.aspx to
www.example.com
Quote:
Quote:
Quote:
>>>without using ISAPI filters?
>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>

ThatsIT.net.au
Guest
 
Posts: n/a
#9: Dec 3 '07

re: How to 301 redirect default.aspx to root


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" <davembush@dmbcllc.comwrote in message
news:623D89CCDA0840B38E99691CFA321876@OfficeVista. ..
Quote:
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:eirik@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" <nomailreplies@nowhere.comskrev i melding
news:ePYWQ8CMIHA.3852@TK2MSFTNGP06.phx.gbl...
Quote:
>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
>
Quote:
>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" <eirik@increo.nowrote in message
>news:edCBwHCMIHA.820@TK2MSFTNGP06.phx.gbl...
Quote:
>>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
Quote:
Quote:
>>www.eample.com/default.aspx and www.example.com. I want to 301
redirect
Quote:
Quote:
>>anyone who get default.aspx in the url.
>>>
>>Request.RawUrl returns default.aspx in both cases, so i can't figure
out
Quote:
Quote:
>>how to check if the user has default.aspx in his url.
>>>
>>>
>>>
>>"Andrew Brook" <ykoorb@hotmail.comskrev i melding
>>news:%232pj3eBMIHA.3940@TK2MSFTNGP05.phx.gbl.. .
>>>If i recall correctly, can't you just set this up in IIS, i just
googled
Quote:
Quote:
>>>"301 redirect" and got
>>>http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
>>>link...
>>>>
>>>:)
>>>Andrew
>>>>
>>>"Eirik Eldorsen" <eirik@increo.nowrote in message
>>>news:ef1m%23WBMIHA.4272@TK2MSFTNGP05.phx.gbl. ..
>>>>How can I 301 redirect www.example.com/default.aspx to
www.example.com
Quote:
Quote:
>>>>without using ISAPI filters?
>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>
Juan T. Llibre
Guest
 
Posts: n/a
#10: Dec 3 '07

re: How to 301 redirect default.aspx to root


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:2DFD22EA-87A7-499D-94B7-41FC8CC3DC3A@microsoft.com...
Quote:
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" <davembush@dmbcllc.comwrote in message news:623D89CCDA0840B38E99691CFA321876@OfficeVista. ..
Quote:
>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:eirik@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" <nomailreplies@nowhere.comskrev i melding
>news:ePYWQ8CMIHA.3852@TK2MSFTNGP06.phx.gbl...
Quote:
>>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
>>
Quote:
>>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" <eirik@increo.nowrote in message
>>news:edCBwHCMIHA.820@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
Quote:
>>>www.eample.com/default.aspx and www.example.com. I want to 301
>redirect
Quote:
>>>anyone who get default.aspx in the url.
>>>>
>>>Request.RawUrl returns default.aspx in both cases, so i can't figure
>out
Quote:
>>>how to check if the user has default.aspx in his url.
>>>>
>>>>
>>>>
>>>"Andrew Brook" <ykoorb@hotmail.comskrev i melding
>>>news:%232pj3eBMIHA.3940@TK2MSFTNGP05.phx.gbl. ..
>>>>If i recall correctly, can't you just set this up in IIS, i just
>googled
Quote:
>>>>"301 redirect" and got
>>>>http://www.webconfs.com/how-to-redirect-a-webpage.php as the first
>>>>link...
>>>>>
>>>>:)
>>>>Andrew
>>>>>
>>>>"Eirik Eldorsen" <eirik@increo.nowrote in message
>>>>news:ef1m%23WBMIHA.4272@TK2MSFTNGP05.phx.gbl.. .
>>>>>How can I 301 redirect www.example.com/default.aspx to
>www.example.com
Quote:
>>>>>without using ISAPI filters?
>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>

ThatsIT.net.au
Guest
 
Posts: n/a
#11: Dec 22 '07

re: How to 301 redirect default.aspx to root



"Juan T. Llibre" <nomailreplies@nowhere.comwrote in message
news:OUczj5bNIHA.3400@TK2MSFTNGP03.phx.gbl...
Quote:
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

Quote:
>
>
>
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:2DFD22EA-87A7-499D-94B7-41FC8CC3DC3A@microsoft.com...
Quote:
>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" <davembush@dmbcllc.comwrote in message
>news:623D89CCDA0840B38E99691CFA321876@OfficeVista ...
Quote:
>>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:eirik@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" <nomailreplies@nowhere.comskrev i melding
>>news:ePYWQ8CMIHA.3852@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" <eirik@increo.nowrote in message
>>>news:edCBwHCMIHA.820@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" <ykoorb@hotmail.comskrev i melding
>>>>news:%232pj3eBMIHA.3940@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" <eirik@increo.nowrote in message
>>>>>news:ef1m%23WBMIHA.4272@TK2MSFTNGP05.phx.gbl. ..
>>>>>>How can I 301 redirect www.example.com/default.aspx to
>>www.example.com
>>>>>>without using ISAPI filters?
>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>
>
Closed Thread