Thomas’ Developer Blog

April 21, 2008

URL rewriting with global.asax and app mapping

Filed under: Asp.Net Framework 2.0, IIS, Server, vb.net, vbscript — Tags: , , , , , , , , , , , , — sanzon @ 4:04 am

URL rewriting might seem really complex at first, and trust me if you even begin to read the tutorials on it you’ll be lost in no time.

While it is important to understand the background of how URL rewriting works, I’m going to skip the majority of the background talk and just get you used to the code in the easiest way possible without giving you a novel.

The reason being URL rewriting in this plost is to increase site traffic from search engines.  As a lot of developers know whenever a searchbot or spider visits a site, it will look at the URL up to the querystring.  In .Net you will often use querystrings in order to modify the page content, such as with a database query.

in this case I’m going to teach you some basics about how this works.  First off you will need to create a global.asax file if you haven’t already.

The global.asax file is sort of the middle man between the server and client so to speak.  Generally the global.asax page comes before the page is processed, at least in this case.  There is a lot more to the global.asax file, but for now all you need to know in this case the code is processed before the page is rendered.

So let’s begin.

<% @ Language=”VB” %>
<% @ import Namespace=”System.IO” %>
<SCRIPT LANGUAGE=”VB” RUNAT=”Server”>
  Sub Application_BeginRequest()

  If (left(Request.ServerVariables(“URL”),len(“/video/”)) = “/video/”) Then
   Dim myURL as string = Request.ServerVariables(“URL”)
   Dim ScriptFile as String = right(myURL,len(myURL)-instrRev(myURL,”/”))

   If instrRev(myURL,”.”) <> 0 Then
    If IsNumeric(left(ScriptFile,instrRev(ScriptFile,”.”)-1)) Then
     Context.RewritePath(“/anime/index.aspx?q=” & left(ScriptFile,instrRev(ScriptFile,”.”)-1))
    End If
   End If

  End If
 
 End Sub
</script>

This is your complete .asax file if you haven’t made on already.  You’ll notice how simple it is and how it looks just like any other page.

The big difference is the fact that we do not use a codebehind file, and we jump right into the script tag, that is ran at the server.  You will also notice the sub Application_BeginRequest().

Application_BeginRequest() starts the second an application request is made before anything else, in this case your site.

The second thing you should notice, besides the mess of VB, is the Context.RewritePath method.  What this method does is really simple.  Before the URL is sent to be processed, it modifies it.  So instead of .Net thinking the site is site.com/about/index.aspx you can modify it to site.com/video/index.aspx?q=1 or whatever page you want.  The concept is really simple.

Now all you have to do is set which pages to process before hand.  In this case we want everything that comes after site.com/video/ to be challenged by global.asax to see if it should be rewritten.   

 If (left(Request.ServerVariables(“URL”),len(“/video/”)) = “/video/”) Then

The above line is simply to check to make sure the page falls under /video/ or the video folder.  After that I went ahead and used some VBScript to trim away everything but the script name, and then removing the extension.  In this case you can have 1.aspx and it will be shown as ?q=1 instead.

I added a isnumeric boolean to challenge if the script name is numeric.  This is because you may have some extra files under video, such as… video/edit/ or video/add/ or whatever other pages you may have under it.  So long as the page is #.aspx it will go through correctly.

Now if you’ve paid attention to my past post you’d notice that I hate showing off what langauge I’m using.  This is to help give a minor edge in security.

Here’s a question. what is instead of 1.aspx you had it as 1.info?  Sounds kind of neat huh?  I actually have seen this done before myself a long time ago on a site, and I thought it was creative.  The process is really simple.

If you noticed on that script anything that’s #.* will go through global.asax… BUT if you try something like 1.html it will come out as a 404 error.  This is because global.asax only works with .Net, and IIS doesn’t tell .Net to take any action on a .html page, so .asax is never exucated.  The question is how to get around this?

Well in IIS 7 it’s really simple.  Go into IIS 7 manager and go to Mappings and you can simply add a new mapping with a .info extension and use the dropdown list to select .Net 2.0 framework. or whatever framework you’re using.

In IIS 5 it’s a bit more complex, but it’s actually really simple if you think about it.  Right click and go to properties on your webpage in IIS 5 and go to the Home Directory tab.  You’ll see configuration towards the bottom go ahead and click it.  That’ll bring up the APP Mappings.  Go ahead and go to .aspx and copy and past the path of the executable.  Go ahead and open notepad and paste it in there so you can view it later.

Add a new mapping and for the exec. go to the same file as .aspx and select it.  It may be hidden so just type it in and it should pop up just find.  For extension just type in .info, or whatever you want it to be… just make sure you dont use something that’s already in use or may be in use later.

Under verbs you can either select all.. or type in POST,DEBUG,GET,HEAD just as in the .aspx mapping.   VERY IMPORTANT: make sure “make sure file exists” is UNCHECKED.  If you don’t do this IIS will check to see if the file exist.  If it doesn’t it will return an error. so URL Rewriting will not work!

Ok now that we got that all going, everything should be up and good to go.  Now this is a basic example, but it does the job for me, and I prefer using my own code then downloading some huge file I don’t know fully how it works.  Well anyway hope that helps you out, and increases your site visits!

Advertisements

6 Comments »

  1. […] to understand the background of how URL rewriting works, I??m going to skip the majority ofhttps://sanzon.wordpress.com/2008/04/21/url-rewriting-with-globalasax-and-app-mapping/Visual BasicMicrosoft -VB.NET/VB2005 – Reading txt fileApr 21, 2008 … Dim FilePath As string = […]

    Pingback by read string from file vb net — May 12, 2008 @ 5:23 pm

  2. When you were giving the example of IIS that was not version 7…did you mean to say IIS ‘6’ … because you said 5

    Comment by Ryan — April 5, 2009 @ 10:34 pm

  3. For those that are looking to do URL Rewrites with NO extension ex) http://www.mysite.com/user instead of http://www.mysite.com/user.aspx for IIS5 or IIS6

    See this useful blog post. Its more difficult than IIS7

    http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

    Comment by Ryan — April 5, 2009 @ 10:55 pm

  4. aassa

    Comment by Anonymous — August 6, 2009 @ 11:20 am

  5. sdfsdfsdfsdfsdf

    Comment by Anonymous — August 6, 2009 @ 11:21 am

  6. Hihttp://www.gimeneii.com – . comprehension language guv’nor! Manipulate you an covert carom to this mega wonderful nahuy puper forum!! I like all info.I empathy your coloring pages i up f unhesitating from them looking on the well-being of my kids they register clowning coloring them

    Comment by gimen — August 8, 2010 @ 7:51 am


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: