2/16/2006 11:56:41 AM
This code is will let you get a glimpse of a URL rewriting engine. There are those of us that don't have the luxury of being able to host our own sites. If you have any questions or can see any way to make this more efficient please let me know.
The Application_BeginRequest section is an ideal location because like the name specifies, this is the first event in the HTTP pipeline chain of execution. When using a Global.asax file, every ASP.NET request response will first start here.
In this code you will noticed the NameValueCollection called blogConfig. Keep in mind that this code was taken from my own application. I happen to keep the path of entry.aspx in an XML configuration file. This is a very easy change to make. Just comment out the blogConfig declaration and change strEntryPath to equal the location of your entry / employee / product / article / etc... ASPX file.
The Regular Expression in strPattern will return a result for the following URL schemas:
http://www.domain.com/stories/archive/story_about_this_444.aspx
http://www.domain.com/blog/210.aspx
http://www.domain.com/hey_this_works_too_post_00222.aspx
The ID's that are picked up will be 444, 210, and 222.
Global.asax
Sub Application_BeginRequest(ByVal sender as Object, ByVal e as EventArgs)
Dim httpContext As System.Web.HttpContext = httpContext.Current()
Dim currentURL As String
Dim strPattern As String = "000(\d+).aspx"
If Request.QueryString("entryID") > 0 Then
currentURL = "000" & Request.QueryString("entryID") & ".aspx"
Else
currentURL = Request.Path.ToLower()
End If
Dim blogConfig As NameValueCollection
blogConifg = ConfigurationSettings.GetConfig("settings/blogConfig")
Dim strEntryPath As String
If blogConfig("entryPath") = "" Then
Response.Write("Please add blog path to configuration file")
Return
Else
strEntryPath = blogConfig("entryPath").ToString()
Dim objRegEx As Regex
Dim Matches As MatchCollection
Dim pageID As String
objRegEx = New Regex(strPattern, RegexOptions.IgnorePatternWhitespace)
Matches = objRegEx.Matches(currentURL)
If Matches.Count > 0 Then
pageID = Matches(0).Groups(1).ToString()
httpContext.RewritePath(strEntryPath & "?entryID=" & pageID)
Else
httpContext.RewritePath(currentURL)
End If
End If
End Sub
All that's left to do now is programmatically create static URL's. What I did was run a replace function on my post title stripping illegal characters away and then concatenating the entryID to the end and add .aspx.
Please feel free to e-mail me or post questions relating to this matter and I will try my best to answer them.
Happy programming!
Code,
VB.NET

Comments

OK, this I`ve been able to do. My question is: Have you figured out (or know anyone that has) how to get an url like "www.url.com/subject" to work without any actual file name or ending slash entered?
I`ve seen this all over the place and just want to enrich the brain.
J
Posted by: Jacko | 3/14/2006 2:02:40 AM

Jacko,
I`ve been wondering the same myself. I have actually toyed with the idea and think I am close to figuring it out (or at least on the right track)!
The only way that I can see this happening is rewriting the post title as a "slug" and store it in a database field. So the post titled "URL Rewriting with .NET" would be slugged "url-rewriting-with-net" right? Now in Begin_Request you can get the server name and path info variables and do an index of to get the post slug located at domain.com/post-slug-here. Now you could perform an ExecuteScalar() to get the post id for this post and rewrite the path. Bam.
This works in theory and I understand that it might be highly inefficient. If you have any input please let me know.
- Will
Posted by: Will | 3/14/2006 8:52:26 PM

I don`t believe you will be able to create URI`s such as http://www.me.com/my-latest-blog, at least without messing with IIS. The only way your global.asax file will be called is from the .NET engine which is called by IIS. IIS decides to call the .NET engine based on the file extension. So, if IIS doesn`t see that initial .aspx (or any registered .net extension) it doesn`t call the .NET engine.
This is one of the pitfalls of not having access to IIS. Obviously if you had access to IIS, you could simply make .NET the default engine for all incoming requests.
If you learn otherwise I`d be very interested to see. Good luck.
Posted by: Bobby | 3/29/2006 10:21:31 PM

Thanks for the reply Bobby. The only other way I can think to do it is save a post-slug and look for that in the URI Index.
- Will
Posted by: Will | 3/29/2006 11:44:13 PM

I`m not quite sure I understand what a post-slug is. It seems to me that regardless of what is it, you still need that extension or access to IIS to have it intercept it.
Perhaps you could elaborate on the post-slug.
Thanks,
-Bobby
Posted by: Bobby | 3/30/2006 10:51:17 PM

Let`s say that you`re post title is: Testing URL Rewriting Possibility. When I submit this post for publication I run the title through a method that strips unwanted characters (i.e. ^#$, etc...) and replaces the spaces with - so that the created string looks like testing-url-rewriting-possibility.
That is called a slug, an identifier of an entry. Now there can be functionality involved in creating this method where it searches the database for a similar post-slug and if one is found it concatentates an integer onto the end i.e. (pseudo-code)
if (ds.Tables[0].Rows.Count > 0)
{
_postSlug = _postSlug + "-" + ds.Tables[0].Rows.Count + 1;
}
So now the post-slug would be testing-url-rewriting-possibility-2 (if there was 1 already listed), but I digress. This post-slug would be written to the database in a field called "name here".
So now you have this post slug attributed to a certain entry and if done correctly each entry will have a unique post-slug. Now all you need to do is edit your Global.asax file to look for the this in the index. I forget off the top of my head what the correct syntax is but it`s something along the lines of IndexOf(...). So you would then do a replace function to get the slug (testing-url-rewriting-possibility) and then pass this string to a method that will perform an ExecuteScalar() to retrieve the ID of this entry. If there is no return then you simply don`t have to rewrite the path, else you concatentate the returned value (ID) to your rewrite path line.
Sorry if this is kind of complicated. I tend to think and type really fast and might have overlooked something. If you want, I can point you in the direction of getting the code to search in a URL string for indexes.
I hope this helps.
Posted by: Will | 3/30/2006 11:39:52 PM

That makes sense - guess I just didn`t know the correct terminology. I`ve done that very thing on my person website except instead of using a title I use the date it was posted. For example an entry might have a link to http://www.bobbyearl.com/2004/12/27/default.aspx None of those directories even exist.
However, that doesn`t show what I`m talking about and what the original question was - how to get to a page without including the file. In my case it`s default.aspx, in your`s it`s the entry`s id .aspx.
You then say that in your global.asax file to check for the "slug`s" id and redirect to the appropriate page (like article.aspx?ID=2); however, what I`m saying is that without changing your IIS settings your global.asax file will not be called.
As my previous example, try to visit a specific entry in the ".cache" on my website. You`ll notice the url as I`d pointed out. Now remove the default.aspx and kaboom - directory not found. The .NET ISAPI filter doesn`t automatically process all requests unless you tell it to in IIS.
Posted by: Bobby | 3/31/2006 4:23:19 PM

Hi,
I need to create a sample URL Rewriting Component for my Company.Please tell me how to go about it.
Thanks,
Ashish
Posted by: Ashish | 9/30/2006 1:26:46 AM

"The .NET ISAPI filter doesn`t automatically process all requests unless you tell it to in IIS."
I can’t find the setting to do that? Could you please tell me where to find it?
Posted by: Tom | 1/22/2007 11:51:42 AM

Ah I found the setting! On the following site is explained how.
http://scottwater.com/blog/pages/Wildcard/
Posted by: Tom | 1/23/2007 4:33:22 AM

In this page i display Main category...
when i click the main or subcategory the url rewrite the categoryname
example:
I have a URL like this : "http://localhost:1324/RL/Category.aspx?Paraid=14&Name=CINEMA"
But, I have `CINEMA` is parentcategory of `ARTS`.
Using URL Mapping Concept to change the URL:
"http://localhost:1324/RL/Category/ARTS/Cinema" but, i need a querystring value `Paraid` also in a hidden form format
Creating a URLMapping is Dynamically..........
It Needs very urgent code for above Format....
Posted by: dhanasekaran | 1/24/2007 2:12:04 AM

Hi there,
I just found your code for "global.asax"
What should we add to config file excatly according to your sample ? can you give me a sample for config file too ?
Regards,
DArgy
Posted by: Dargy | 4/17/2007 11:08:06 PM

I applaud the effort.The only way your global.asax file will be called is from the .NET engine which is called by IIS.
Posted by: Steve M. | 9/15/2007 1:53:05 AM

You can try any url, if the following is true:
1) The url should begin with a path to an existing, real virtual directory
2) The url should end with aspx. Thats all.
You can play with it and do anything you want. Forget everything else.
Posted by: Tinyavatar | 12/13/2007 11:06:31 AM

I just found your code for "global.asax"
Posted by: Johny | 2/20/2008 2:32:19 PM

I am Very thank full the owner of this blog. Becouse of this blog is very imformative for me.. And I ask u some thiing You make more this type blog where we can get more knowledge.
Posted by: Prosolution | 4/14/2008 2:49:13 AM

I am Very thank full the owner of this blog. Becouse of this blog is very imformative for me.. And I ask u some thiing You make more this type blog where we can get more knowledge.
Posted by: Prosolution | 4/14/2008 2:52:46 AM

I am Very thank full the owner of this blog. Becouse of this blog is very imformative for me.. And I ask u some thiing You make more this type blog where we can get more knowledge.
Posted by: Prosolution | 4/18/2008 6:58:13 AM

thanks
Posted by: kral oyun | 11/4/2008 1:39:16 PM