Musings of a technophile

Just another blog from a computer nerd
  • Home
  • About Me
  • Funny things my kids say

Fixing 404 errors on invalid URL’s with ASP.NET’s URL Rewrite

ElementZero | November 13, 2008

So my boss where I work noticed that Google was showing 404 error’s in our Google webmaster tools, and that many of those 404′s are on invalid URL’s. With a site as big as ours, I wouldn’t say it would be uncommon to have other sites linking to our content and they mess up the URL in the link, but in any case since we are big on SEO and making sure every user has a good experience on the site, we often work to try and make these 404′s into working URL’s. That being said, the problem URL’s this time around were ones that had characters on the end of the URL. So for instance while our main page is “index.aspx”, we would have 404′s on “index.aspx;” or “index.aspx,”. Obviously the problem is the funky characters on the end that shouldn’t be there, and we needed to remove them. I instantly though URL rewrites would be a good way to fix this, so I wrote up the following script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script runat="server">
Private Sub Application_OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        Dim application As HttpApplication = CType(sender, HttpApplication)
        Dim strRegex As String = "\.aspx[;,()!^]+$"
        Dim RegexObj As System.Text.RegularExpressions.Regex = New Regex(strRegex)
        Dim url As String = application.Request.Url.AbsolutePath
        If RegexObj.IsMatch(url) Then
            url = Regex.Replace(url, strRegex, ".aspx" )
            HttpContext.Current.Response.Status = "301 Moved Permanently"
            HttpContext.Current.Response.AddHeader("Location", url)    
        End If        
        HttpContext.Current.RewritePath(url)        
    End Sub
</script>

Now you would just put all of this into your Global.asax file. The Global.asax (for those that don’t know) executes on every request, and therefore every url that is submitted to the site will execute through this function.

Basically this function will take the url passed in, and if it contains one or more of the following characters

;,()!^

after a “.aspx” it will remove them, and then bring the user to the fixed URL. Note that there may be more special characters you can add other than this, but these are all I tested with. Also note that characters such as a period and a colon will not work as they are special URL characters already, and apparently the server will try to render the URL even before calling the OnBeginRequest function. Also note that this does not fix query strings and/or strange characters after a query string.

The function will also throw a 301 permanent redirect with a location of the fixed URL – this is for the search engines so that they know the page is not valid and where to go instead (basically for SEO).

In any case, this may not be the perfect solution (then again, I don’t know what else could be implemented…perhaps an ISAPI filter?), but it does cover most instances or bad URL’s linking to your site.

Categories
Computer Troubleshooting
Tags
ASP.NET, SEO
Comments rss
Comments rss
Trackback
Trackback

« Outlook Task Manager in Google Sidebar Macross Frontier »

Leave a Reply

Click here to cancel reply.

Categories

  • Anime
    (8)
  • Computer Troubleshooting
    (24)
  • Games
    (17)
  • Family and Everyday Life
    (25)
  • Misc Thoughts
    (13)

Search

Archives

  • June 2010 (2)
  • May 2010 (7)
  • April 2010 (1)
  • January 2010 (1)
  • November 2009 (1)
  • July 2009 (1)
  • May 2009 (1)
  • April 2009 (3)
  • March 2009 (5)
  • February 2009 (8)
  • January 2009 (11)
  • December 2008 (12)
  • November 2008 (12)
  • October 2008 (11)
  • September 2008 (8)

Tags

Apple ASP.NET Blizzard Cisco coding computer repair Disney ecchi Ethan Exchange firewall Food Fort Myers FPS furniture hacking iis 7 jokes Kaden linux Logan Madelyn movies networking Racing RTS SEO Server Administration Shonen SQL SSRS 2005 StarCraft II Warcraft III Windows Server 2008 WordPress Zoo
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox Admin Login