Programming SEO – Tags and Keywords

ka_vol1_121 Last week I started a series on SEO from a programming perspective.  Today we want to look at one of the topics I discussed in that post in a little more detail.  Tags.

Strictly speaking, tagging a page on your web site will have little or no effect on your ranking.  However, tags will be the cornerstone of a lot of other elements of our on-site optimizations as you will discover in future post.  So the first thing we need to add to our site is the ability to add tags to our pages and the ability to make those tag URLs display useful content.

There are numerous ways you might achieve adding keywords to your page.  If your site is all generated dynamically, you might just make the keywords a field.  This is what I did in my last project.

On other pages, you might add them to the keywords meta tag.  Each method will require a different kind of processing.  But no matter which way you implement getting the keywords on the page, you’ll still end up with a URL that looks something like this

http://blog.dmbcllc.com/tag/keyword

The problem you will most likely have is that you probably don’t have a directory on your site that looks like that, meaning that link will generate a 404 error on your site.  Not good.

So, we need some way of making that page exist, even though there is nothing physically on the server to display at that location.  The solution is URL Rewrites.

URL Rewrites involve:

  • Trapping 404 errors
  • Clearing the error
  • Redirecting the request to another URL that does exist

The effect is that the URL still looks like the original URL in the browser, but the server is serving up a URL at a completely different location.  I’ve discussed this trick before at Virtual Files using HttpContext.RewritePath()

Since we are referring to a directory and not an ASPX file, we will need to set a 404 error handler in IIS.  I discuss this in the RewritePath() article.  You should note that  IIS 6 let you specify an ASPX file that didn’t exist.  In IIS 7 you seem to need to point to a file that does exist on the server.  You might consider using a default error handler aspx file.  The code we are implementing here will never get there so it won’t really matter if you use it other than using it for 404 errors that you don’t trap and rewrite.

The next thing you’ll want to do is go to your global.asax file and put some code in your Application_BeginRequest() event handler.

The first thing we need to test for is if we are dealing with a 404 error.  You can do this by checking for the “404;” string in the query string.

if (Request.ServerVariables["QUERY_STRING"]
    .Length > 4
    && Request.ServerVariables["QUERY_STRING"]
    .StartsWith("404;"))

if it is a 404 error, you’ll want to pull out the stuff after the tag string.  The following is generic code I use to normalize the information from the query string so that I can deal with the directory and file name as thought that was the only thing that was sent.

This may seem like a bit of work when you might just search for “/tag/” in the query string, but I do this often enough that I pretty much copy and paste the code to each of my projects that needs it and it ends up making the rest of the code I have to write easier to write.

Here’s the snippet

String queryString =
    Request.ServerVariables["QUERY_STRING"]
    .Substring(4).ToLower();
// strip off the http:// or https://
queryString = queryString
    .Replace("http://", "")
    .Replace("https://", "");
// strip of the host section
queryString =
    queryString
    .Substring(queryString.IndexOf("/") + 1);
string applicationDirectory =
    Request.ApplicationPath;
if (applicationDirectory.StartsWith("/"))
    applicationDirectory =
        applicationDirectory.Substring(1);
queryString =
    queryString
    .Substring(applicationDirectory.Length);
if (queryString.ToCharArray()[0] != '/')
    queryString = "/" + queryString;

Then it’s just a matter of striping off the tag and sending the keyword to an ASPX page for processing using the RewritePath()

if(queryString.StartsWith("/tag/"))
{
    HttpContext.Current.ClearError();
    string keyword = queryString.Substring(5);
    if (keyword.EndsWith("/"))
        keyword = keyword.Substring(0, keyword.Length - 1);
    HttpContext.Current
        .RewritePath("Tag.aspx?keyword=" + keyword,true);
}

In my implementation, Tag.aspx is a page that displays the title of the article and a brief description of the article.  The title links to the actual article on my web site.

By implementing this one feature, you automatically gain three SEO benefits.  First, you’ve created more pages on your site for every keyword you use.  Second, you create keyword-rich content on each of those pages.  Third, you create links back to the pages those keywords are on.

A final note.  You may have some trouble with the ~ “application path” specifier resolving incorrectly in these pages.  This is because of some quirk in ASP.NET.  If you run into this problem, check out my previous article, “UrlRewrite – Virtual Directories and the Tilde” for the fix.


Some Other Resources You May Be Interested In

Getting links back to your site is one of the best ways of generating search position.  These resources can help you with that.

  • MyArticleNetwork – Create an article and have unique versions distributed to multiple blogs.
  • Article Submitter – Send articles to the various article directories.  There is a free version of this software, but the paid version will more than make up for itself in the time you save.  Once your article is in the directory other sites will pick it up generating more links to your site.
  • Directory Submitter – Get links to your web site from the multiple directory services that are available.  Once again, there is a free version of this software, but you’ll get much better mileage from the paid version.

Related Post

Leave a Reply

Comment Policy:

  • You must verify your comment by responding to the automated email that is sent to your email address. Unverified comments will never show.Leave a good comment that adds to the conversation and I'll leave your link in.
  • Leave me pure spam and I'll delete it.
  • Leave a general comment and I'll remove the link but keep the comment.

Notify me of followup comments via e-mail

Bear