Regular Expression Help (URL Rewriting)

W

Will Chamberlain

I'm working on a URL rewriting engine at the moment and would like to
make it more dynamic. I am doing this via Global.asax and as of now I
have to manually write the URL's one-by-one. I am wanting to use a RegEx
on a string and then access the dynamic page this way. For example:

Let's say that the static URL I implement is:

Wonkas_widgets_factory_Tonka_Truck_002.aspx

I would want this to point to:

product.aspx?productID=002

To accomplish this I would simply need to use the RegEx:
\d{3}

Now let's say that I have the following code in Application_BeginReguest
(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 = Request.Path.ToLower()

If currentURL.IndexOf("Willy_Wonkas_widget_factory_001") > 0 Then
httpContext.RewritePath("product.aspx?productID=001")

End Sub

What would be the best way to implement this RegEx so that I can create
many static URL's?
 
E

Electrified Research

I'm sure I can help with regular expressions, but I don't understand
what you're asking.
 
W

Will Chamberlain

Sorry,

I couldn't figured this out earlier. I should've posted a reply to
myself.

Sub Application_BeginRequest(ByVal sender as Object, ByVal e as
EventArgs)

Dim httpContext As System.Web.HttpContext = httpContext.Current
Dim currentURL As String = Request.Path.ToLower()

Dim strPattern As String = "page(\d+).aspx"

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("product.aspx?productID=" & pageID)
Else
httpContext.RewritePath(currentURL)
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

URL rewriting 6
rewriting URL 8
url rewriting 3
URL Rewriting using IHttpHandlerFactory 0
URL Rewriting and Caching 0
Regular Expression for URL 4
url rewrite 4
url rewriting 0

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top