Wednesday, July 23, 2008

Generate URL - SEO Friendly Asp.net

Generate SEO Friendly URL from given string in C# for Asp.net Web Application.

Example 1: Simple Test

  • Input: CSS is not working in IE? but working in FireFox... asp.net problem
  • Output: css-is-not-working-in-ie-but-working-in-firefox-Asp-Net-problem

Example 2:
Spaces Testing
  • Input: asp.net problem url re-writing.... testing the basics
  • Output: Untitled PageUntitled PageAsp-Net-problem-url-re-writing-testing-the-basics

Untitled PageExample 3: Special Character Testing
  • Input: 23 asp.net problem ### url re-writing.... testing the basics will pay $1000
  • Output: Untitled PageUntitled PageUntitled Page23-Asp-Net-problem-url-re-writing-testing-the-basics-will-pay-1000


private string GenerateURL(string strTitle)
{
//Trim Start and End Spaces.
strTitle = strTitle.Trim();

//Trim "-" Hyphen
strTitle = strTitle.Trim('-');

strTitle = strTitle.ToLower();
char[] chars = @"$%#@!*?;:~`+=()[]{}|\'<>,/^&"".".ToCharArray();
strTitle = strTitle.Replace("c#", "C-Sharp");
strTitle = strTitle.Replace("vb.net", "VB-Net");
strTitle = strTitle.Replace("asp.net", "Asp-Net");

//Replace . with - hyphen
strTitle = strTitle.Replace(".", "-");

//Replace Special-Characters
for (int i = 0; i < chars.Length; i++)
{
string strChar = chars.GetValue(i).ToString();
if (strTitle.Contains(strChar))
{
strTitle = strTitle.Replace(strChar,string.Empty);
}
}

//Replace all spaces with one "-" hyphen
strTitle = strTitle.Replace(" ", "-");

//Replace multiple "-" hyphen with single "-" hyphen.
strTitle = strTitle.Replace("--", "-");
strTitle = strTitle.Replace("---", "-");
strTitle = strTitle.Replace("----", "-");
strTitle = strTitle.Replace("-----", "-");
strTitle = strTitle.Replace("----", "-");
strTitle = strTitle.Replace("---", "-");
strTitle = strTitle.Replace("--", "-");

//Run the code again...
//Trim Start and End Spaces.
strTitle = strTitle.Trim();

//Trim "-" Hyphen
strTitle = strTitle.Trim('-');

return strTitle;
}

7 comments:

Arthur Chaparyan said...

Isn't there some sort of regular expression that can do this in a single statement?

DotNetGuts said...

Yes regular expression is the best way to tackle this, but if you focus on code logic, you wil feel it would consume more time to develop this stuff with regular-expression (as I am not good with it).

DotNetGuts said...

Check out article on URL Rewriting Simplest Article for URL Rewriting

Anonymous said...

Using string.replace multiple times in ADDITION to the loop seems a bit expensive.

Take a look at http://horizonguy.wordpress.com/2008/10/18/optimize-a-url-for-seo-using-c

It's using hash table for char lookups and buffer for building the url.

Ray said...

try this:

using System.Text.RegularExpressions;

public string GenerateURL(string str,bool toLowerCase)
{
//Replace . with - hyphen
str = Regex.Replace(str.Trim(), @"\W", "-");

//Replace multiple "-" hyphen with single "-" hyphen.
str = Regex.Replace(str, @"\055+", "-").Trim('-');

if (toLowerCase)
return str.ToLower();
else
return str;

}

DotNetGuts said...

Hi Ray,

Your solution is helpful and easy. Thanks :)

Asif said...

I just created simple function.

public static string createURL(string m_strURL)
{
//m_strURL = m_strURL.Replace("&", "and");
m_strURL = Regex.Replace(m_strURL, @"[^\w]", "-");
do
{
m_strURL = m_strURL.Replace("--", "-");
} while (m_strURL.Contains("--"));

if (m_strURL.EndsWith("-"))
m_strURL = m_strURL.Substring(0, m_strURL.Length - 1);

if (m_strURL.StartsWith("-"))
m_strURL = m_strURL.Substring(1, m_strURL.Length - 1);

return m_strURL.ToLower();
}

Most Recent Post

Community Updates

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape