Forum Moderators: open

Message Too Old, No Replies

ASPX url rewrite

         

Adam_C

10:41 am on Apr 19, 2004 (gmt 0)

10+ Year Member



Does anyone have any info on rewriting dynamic to static looking URLs for a site using ASPX technology?

f00sion

3:21 am on Apr 20, 2004 (gmt 0)

10+ Year Member



I dont think there is anything much different that you can do with .net as far as url rewriting over the classic asp methods. the best solution is to use an isapi filter such as isapirewrite or iisrewrite.

Adam_C

8:56 am on Apr 20, 2004 (gmt 0)

10+ Year Member



OK - cheers.

This is not my area of speciality, but have loads of info on isapirewrite, etc.

TheWhippinpost

10:09 am on Apr 20, 2004 (gmt 0)

10+ Year Member



Try this - I've not used it, I grabbed it out of me snippet file.

In your Global.asax file, modify the Application_BeginRequest event as the following:


protected void Application_BeginRequest(Object sender, EventArgs e)
{

HttpContext incoming = HttpContext.Current;
string oldpath = incoming.Request.Path.ToLower();
string pageid; // page id requested

// Regular expressions to grab the page id from the pageX.aspx
Regex regex = new Regex(@"page(\d+).aspx", RegexOptions.IgnoreCase
¦ RegexOptions.IgnorePatternWhitespace);
MatchCollection matches = regex.Matches(oldpath);

If (matches.Count > 0)
{
// Extract the page id and send it to Process.aspx
pageid = matches[0].Groups[1].ToString();
incoming.RewritePath("Process.aspx?pageid=" + pageid);
}
else
{
// Display path if it doesn’t contain pageX.aspx
incoming.RewritePath(oldpath
}
}


In the above code, URLs of incoming requests are scanned to determine if the URL includes “pageX.aspx”, where X=1,2,3…n

Your code
Next create a file Process.aspx which would display your web site content based on the pageid parameter (pageid=1…n) just like normal.

Eg...


<%
string pageid = Request.QueryString["pageid"];
// Create the page content based on this pageid taken here
%>