GibbsUSA.net Blogs  

Wednesday, September 08, 2010

 Logon   Back to Blog   Blogs Home   GibbsUSA.net Home  


Archives
 Home... 
 May 2008 (1) 
 August 2007 (2) 
 December 2006 (1) 
 October 2006 (5) 
 

Leave your comments...

10/26/2006 4:34 PM: Making URLs search engine friendly

Here is an example of turning a querystring into a search engine friendly URL..

The URL comes from a search engine in the format:

www.gibbsusa.net/develop/rss/blogs/showblog_page-ID-8.aspx

The code converts this transparently to the following URL:

www.gibbsusa.net/develop/rss/blogs/showblog.aspx?ID=8

The following snippet comes from the global.asax file

protected void Application_BeginRequest(Object sender, EventArgs e)
{
	// code to make blogs search engine friendly
	HttpContext incoming = HttpContext.Current;
	string oldpath = incoming.Request.Path.ToLower();
	if(oldpath.ToLower().IndexOf("_page-")>0)
	{
		string pageRedirect = oldpath.Substring(oldpath.LastIndexOf("/")+1);
		pageRedirect = pageRedirect.Substring(0,pageRedirect.ToLower().IndexOf("_page"));
		// extract the items
		string qsItemsTemp = oldpath.Substring(oldpath.ToLower().IndexOf("_page-")+6);
		qsItemsTemp = qsItemsTemp.Substring(0,qsItemsTemp.ToLower().IndexOf(".aspx"));
		string[] qsItems = qsItemsTemp.Split('-');
		string theQString = "";
		for (int i=0;i<=qsItems.GetUpperBound(0);i++)
		{
			if (theQString.Length>0)
			{
				theQString += "&";
			}
			theQString += qsItems[i].ToString() + "=" + qsItems[i+1].ToString();
			i++;
		}
		incoming.RewritePath (pageRedirect + ".aspx?" + theQString); 
	}
	else

	{
		// Display path if it doesn’t contain _pageX.aspx
		incoming.RewritePath(oldpath );
	}
}

Print >>> EMail >>>

Comments:
Please log on to the GibbsUSA site to post comments...

 


CLR v2.0.50727.3053


Please see the site Terms And Conditions and the site Privacy Statement.
This entire web site (and the software contents therein) is Copyright © Chris Gibbs 2000-2010.
Any reproduction or copying is expressly forbidden without the prior consent of Chris Gibbs.
For more information, please contact the site WebMaster (WebMaster@GibbsUSA.net).