As a preface, this is really only a solution for those of us who do not have a dedicated or VPs server. If you have access to the IIS Management console there are much better ways to accommodate the need for URL rewriting.
I am going to demonstrate a simple way to implement this, but there are a number of much more complex ways to utilize this code.
You have an ecommerce site and want to clean up your URLs a bit. A call to products.aspx?ID=123 is not quite as good as widgets.aspx.
No Problem.
The solution is a Global.asax file.
So this file goes into the root of your .NET application.
It starts like this
<script language="vbscript" runat="server"> Sub Application_BeginRequest(Sender As Object, E As EventArgs) ...... End Sub </script>
The Application_BeginRequest is a lot like the page_load event in your standard .NET code except it starts with the application not the individual page.
And as an example if we popped this into our application and made it read
<script language="vbscript" runat="server">
Sub Application_BeginRequest(Sender As Object, E As EventArgs)
response.write ("Hello world")
End Sub
</script>
Then from there on every page that loaded within this application would have the "Hello World" line at the top of the page.
Okay with me so far? Great!
Whats next... well we're talking about rewriting URLs so we need to know how to look at the URL itself.
<script language="vbscript" runat="server"> Sub Application_BeginRequest(Sender As Object, E As EventArgs) Dim httpContext As System.Web.HttpContext = HttpContext.Current Dim currentURL As String = CurrentURL.Request.Path.ToLower() response.write (currentURL) End Sub </script>
Done, now we are showing the URL in place of Hello World, it's really the path rather than the URL, so we don't have to worry about splitting out a bunch of /'s. We should be seeing the path of the current page relative to the global.asax file or the Application root.
I hope I am not losing you here - it doesn't matter, stick with this and you'll get it in the end.
So we've taken our URL and converted it into a string... so we have "products.aspx?ID=123" at the top of our page and it really isn't helping anybody yet...
All we need to do now is instead of calling "products.aspx?ID=123" let's call "widgets.aspx"... and then all we need to do is tell it that "widgets.aspx" = "products.aspx?ID=123"
Here goes.
<script language="vbscript" runat="server">
Sub Application_BeginRequest(Sender As Object, E As EventArgs)
Dim httpContext As System.Web.HttpContext = HttpContext.Current
Dim currentURL As String = CurrentURL.Request.Path.ToLower()
If currentURL.IndexOf("widgets") > 0 Then
objHttpContext.RewritePath("products.aspx?ID=123")
else
objHttpContext.RewritePath(httpContext)
end if
End Sub
</script>
Thats it. One thing to mention here is that you can really, really expand on this.
clicky - widgets.aspx
11/23/2005 11:06:57 PM How about awesome? I`ll try this out tomorrow. Do you mean if I syndicate this on my site? All credit will be given of course.