SEO stands for Search Engine Optimization. It is the art, craft, and science of driving web traffic to web sites.
Learning how to construct web sites and pages to improve and not harm the search engine placement of those web sites and web pages has become a key component in the evolution of SEO.
In this article we will see one of the technique that is used commonly for improving SEO and creating friendly URL.
Today, most of the websites built are database driven or dynamic sites and most of these websites pass data between pages using query strings. Search engines crawlers usually do not index the page having a question mark or any other character. If search engines do not identify a page or its content in a website, it essentially means missing web presence for that page. How this could be handled. This write-up discusses this topic with a sample web site project as implementation reference.
Friendly URLs
Friendly URLs pass information to the pages without using the question mark or any other character and these pages will be indexed by the search engines which will maximize search engine rankings for your website. Search engines prefer static URLs to dynamic URLs.
A dynamic URL is a page address that is created from the search of a database driven web site or the URL of a web site that runs a script. In contrast to static URLs, in which the contents of the web page stay the same unless the changes are hard-coded into the HTML, dynamic URLs are generated from specific queries to a site's database. The dynamic page is only a template in which to display the results of the database query.
Search engines do not index the dynamic URLs for the reason that it will contain the non-standard characters like ?, &, %, = etc. Many times, anything after the non-standard character is disregarded. For example, we may have URLs like:
http://www.myweb.com/default.aspx?id=120
In this case, if the URL after the first non-standard character is omitted, the URL will look like:
http://www.myweb.com/default.aspx
The URLs of this type will be a group of duplicate URLs for the search engine and search engines omit the duplicate URLs and the search engine will not index all your dynamic pages. The search engine indexes a URL like:
http://www.myweb.com/page/120.aspx
Even though nowadays search engines are optimized to index dynamic URLs, they would still prefer static URLs.
Creating SEO Friendly URLs
What if we were to implement this in our projects? Here is a method that could be used to create friendly URLs to boost your Page rank.
In our example, let us try with these URLs:
http://www.myweb.com/Order.aspx?Itemid=10&Item=Apple
http://www.myweb.com/Order.aspx?Itemid=11&Item=Orange
And our objective is to convert it to contextual URLs that resemble:
http://www.myweb.com/shop/10/Apple.aspx
http://www.myweb.com/shop/11/Orange.aspx
What we are going to do is that first we convert the actual URL with the query string to the contextual URL by first getting the mapped URL name for the page from the web.config file and combining the query string values followed by / and finally the item name as the page name. When this converted contextual URL is clicked and the page is navigated the Application_BeginRequest event in the Global.asax file rewrites the contextul URL into the actual URL with the query strings.
Lets get down in detail about how to build the application that is using friendly URLs.
This application simply explains how to create SEO friendly URLs and this is just an idea about how we can rewrite URLs. You can just take the idea and rewrite it in your own way.
The GetContextual URL method
This method takes actual URL that needs to be converted into friendly URL and the name of the page as the parameters. Here in this application we are using this "Order.aspx?Itemid=10&Item=Apple" URL as an example that needs to converted.
The method will be called like GetContextualURL("Order.aspx?Itemid=10&Item=Apple","Apple");
In this method the URL's query string values are being split and formed into new URL with the existing page Order.aspx is mapped to a new page name apple.aspx and the alias name for Order.aspx is being picked from the web.config file. The newly formed URL will be "~/shop/10/Apple.aspx"
The for loop in this method iterates through the app settings of the web.config file and finds the key for the value “order.aspx”. This is for assiging an alias name for the actual order.aspx. If you want more than one pages to be converted than you should have equivalent alias name entries in the web.config file.
/// <summary>
/// Method to convert the actual url into a contextual unique url
/// </summary>
/// <param name="Url">The actual url</param>
/// <param name="PageName">Name of the page</param>
/// <returns></returns>
public string GetContextualURL(string Url,string PageName)
{
string DestID = string.Empty;
string DestAlias = string.Empty;
string Str = Url.Split('?')[1].Split('=')[1] + "/";
string DestPage = Url.Split('?')[0];
string ItemID = Url.Split('?')[1].Split('=')[1].Split('&')[0];
string NewUrl = string.Empty;
//Get all the Key/Value pairs from web.config
string[] KeyList = WebConfigurationManager.AppSettings.AllKeys;
//Iterate the collection to find the specfic key/value pair
for (int KeyCount = 0; KeyCount < KeyList.Length; KeyCount++)
{
DestID = WebConfigurationManager.AppSettings[KeyList[KeyCount]].Trim();
DestAlias = KeyList[KeyCount];
if (DestID.ToLower() == DestPage.Trim().ToLower())
{
break;
}
}
//Form the contextual URL
NewUrl = "~" + DestAlias + ItemID + "/" + PageName + ".aspx";
return NewUrl;
}
Web.Config
In the app settings of the web.config file add the below alias name entry for the order.aspx
<appSettings>
<add key="/shop/" value="order.aspx" />
</appSettings>
Application_BeginRequest Event
This event is fired whenever a request is made to the server and so this event is the appropriate event to rewrite the contextual URL into the actual URL. In this event we are getting the incoming URL and checking whether it is a contextual URL and if it is so then we are splitting the item id and item name from the contextual URL and creating the actual URL with the query string. This actual URL gets processed and the actual page is executed.
protected void Application_BeginRequest(object sender, EventArgs e)
{
//Get the current http context
HttpContext InRequest = HttpContext.Current;
//Get the current path
string OldPath = InRequest.Request.Path.ToLower();
//Check the path whether it is a contextual path
if (InRequest.Request.RawUrl.Split('/').Length > 3)
{
string Path = InRequest.Request.RawUrl.Split('/')[2];
Path = "/" + Path + "/";
string NewPath = "/furl/" + WebConfigurationManager.AppSettings[Path];
string ItemName = InRequest.Request.RawUrl.Split('/')[InRequest.Request.RawUrl.Split('/').Length - 1].Split('.')[0];
string ID = InRequest.Request.RawUrl.Split('/')[3];
//Rewrite the path with the actual path
InRequest.RewritePath(NewPath, "", "?id=" + ID + "&id2=" + ItemName, true);
}
}
Requested Page
In the Page_Load event of the actual requested page order.aspx get the value of the item id and item name from the query string.
Label1.Text = Request.QueryString[1] +", Qty "+ Request.QueryString[0];
Conclusion
To implement Search Engine Optimization (SEO), there are numerous areas to focus Page index, strategies around maximizing page rank etc. However, for a website with dynamic content, letting the page be indexed by the search engines is the primary goal and a good beginning for SEO implementation.