URL Rewriting: site.com/bandname  
Author Message
carolslp





PostPosted: Thu Mar 23 12:58:08 CST 2006 Top

ASP.Net >> URL Rewriting: site.com/bandname

Hi guys

I want to create a situation whereby users can use a band name to access a
data page. So..

site.com/bandname actually goes to site.com/music/artist.aspx?id=100

I currently do this by having loads of directories, but that means loads of
work if I add a new artist, so I wanted to recode it using
Context.RewritePath.

Here's the code I've got in global.asax..

void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext incoming = HttpContext.Current;
string URL = incoming.Request.Path.ToLower();

string reWri = new URLRewriter().RewriteURL(URL).ToString();

if(reWri.CompareTo("") != 0)
incoming.RewritePath(reWri);
}

And the URLRewriter class looks like this...

public class URLRewriter
{
public URLRewriter()
{

}

public string RewriteURL(string URL)
{
// see if we can find an artist, and send back a URL or a blank string
string bandname, retURL="";

OleDbConnection dbConn = new
OleDbConnection(ConfigurationManager.ConnectionStrings["mswDB"].ToString());
dbConn.Open();

// get the artist
OleDbCommand artCmd = new OleDbCommand("SELECT ArtistID, ArtistName
FROM Artists ORDER BY ArtistName", dbConn);
OleDbDataReader artDr = artCmd.ExecuteReader();
while (artDr.Read())
{
bandname = artDr["ArtistName"].ToString().ToLower();
bandname = bandname.Replace(" ","");
bandname = "/" + bandname;

if (bandname.CompareTo(URL) == 0)
retURL = "/_music/artist.aspx?id=" +
artDr["ArtistID"].ToString();
}

artDr.Close();
artDr = null;
dbConn.Close();
dbConn = null;

return retURL;
}
}

I think the code is right - I've had some test stuff in there and I know
it's returning the "_music/artist.." page URL. The problem is that it doesn't
redirect to it. I just get a 404...

Am I missing some code, or do I need to do something in IIS to make this
work?! Any help appreciated!

Cheers


Dan (running Win2k3, IIS6, vs2005, .net2)

Web Programming85  
 
 
George





PostPosted: Thu Mar 23 12:58:08 CST 2006 Top

ASP.Net >> URL Rewriting: site.com/bandname Here are some tips about URL rewriting. Follow them and you will live happy
long life .... :)

1. Url rewriting only works if goes through .NET engine. (Looks like you
have a problem here). In order for your requests to got through .NET engine
they must have aspx extension (there are couple other extensions but I will
not count them here).
That means your request should be not site.com/bandname but
site.com/bandname.aspx

Or if you still insist on having site.com/bandname then you must have a
folder with name bandname and default.aspx in it.

Still not happy? Then have your own custom 404 error as an ASPX page in IIS
console. Sometimes it's not an option with shared hosting plan.

------------------------------------------------------
Do not quit reading. You still not done with rewriting.


2. Once you successfully rewrote your URL to
site.com/music/artist.aspx?id=100 you are going to face second issue. The
ASP.NET is going to work in context of new URL while browser is working with
context of old URL.

Meaning, if you rewrite browser's request site.com/bandname.aspx then
browser is going to try to get images or links that are on that page and
have relative path from site.com/imagepath if you have an .aspx extension or
from site.com/bandname/imagepath if you do not have .aspx extension.

so make sure you are using absolute path in all links or references to
images, .js, .css files ......

2.1. Another problem is that .NET works with new URL once you rewrote it.
Let say you rewrote it to site.com/music/artist.aspx?id=100
Guess what form's "action" property will be set to? Correct answer: it's
going to be set to "site.com/music/artist.aspx". So once your user click
that submit button he is going to see your "ugly" URL in the address bar.
Solution is simple: Rewrite the URL back to original once you hit the page.


Here is sample pseudocode.

void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext incoming = HttpContext.Current;
incomming.Items["OLDURL"] =
ncoming.Request.Path; -----------------NEW CODE
string URL = incoming.Request.Path.ToLower();

string reWri = new URLRewriter().RewriteURL(URL).ToString();

if(reWri.CompareTo("") != 0)
incoming.RewritePath(reWri);
}
---------------------------------------------

Page_OnInit
{
//first thig we do is rewrite back
HttpContext.Current.RewritePath(
HttpContext.Current.Items["OLDURL"].);
}
----------------------------------------

Usually I am using inheritance here and all my pages are derived not from
UI.Page but from my own custom page CustomPage which does that thing.


George.





> Hi guys
>
> I want to create a situation whereby users can use a band name to access a
> data page. So..
>
> site.com/bandname actually goes to site.com/music/artist.aspx?id=100
>
> I currently do this by having loads of directories, but that means loads
> of
> work if I add a new artist, so I wanted to recode it using
> Context.RewritePath.
>
> Here's the code I've got in global.asax..
>
> void Application_BeginRequest(object sender, EventArgs e)
> {
> HttpContext incoming = HttpContext.Current;
> string URL = incoming.Request.Path.ToLower();
>
> string reWri = new URLRewriter().RewriteURL(URL).ToString();
>
> if(reWri.CompareTo("") != 0)
> incoming.RewritePath(reWri);
> }
>
> And the URLRewriter class looks like this...
>
> public class URLRewriter
> {
> public URLRewriter()
> {
>
> }
>
> public string RewriteURL(string URL)
> {
> // see if we can find an artist, and send back a URL or a blank
> string
> string bandname, retURL="";
>
> OleDbConnection dbConn = new
> OleDbConnection(ConfigurationManager.ConnectionStrings["mswDB"].ToString());
> dbConn.Open();
>
> // get the artist
> OleDbCommand artCmd = new OleDbCommand("SELECT ArtistID, ArtistName
> FROM Artists ORDER BY ArtistName", dbConn);
> OleDbDataReader artDr = artCmd.ExecuteReader();
> while (artDr.Read())
> {
> bandname = artDr["ArtistName"].ToString().ToLower();
> bandname = bandname.Replace(" ","");
> bandname = "/" + bandname;
>
> if (bandname.CompareTo(URL) == 0)
> retURL = "/_music/artist.aspx?id=" +
> artDr["ArtistID"].ToString();
> }
>
> artDr.Close();
> artDr = null;
> dbConn.Close();
> dbConn = null;
>
> return retURL;
> }
> }
>
> I think the code is right - I've had some test stuff in there and I know
> it's returning the "_music/artist.." page URL. The problem is that it
> doesn't
> redirect to it. I just get a 404...
>
> Am I missing some code, or do I need to do something in IIS to make this
> work?! Any help appreciated!
>
> Cheers
>
>
> Dan (running Win2k3, IIS6, vs2005, .net2)