<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Musings of a technophile &#187; SEO</title>
	<atom:link href="http://www.omegaprojex.com/index.php/tag/seo/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.omegaprojex.com</link>
	<description>Just another blog from a computer nerd</description>
	<lastBuildDate>Mon, 19 Jul 2010 15:39:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Fixing 404 errors on invalid URL&#8217;s with ASP.NET&#8217;s URL Rewrite</title>
		<link>http://www.omegaprojex.com/index.php/2008/11/13/fixing-404-errors-on-invalid-urls-with-aspnets-url-rewrite/</link>
		<comments>http://www.omegaprojex.com/index.php/2008/11/13/fixing-404-errors-on-invalid-urls-with-aspnets-url-rewrite/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 14:00:52 +0000</pubDate>
		<dc:creator>ElementZero</dc:creator>
				<category><![CDATA[Computer Troubleshooting]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[SEO]]></category>

		<guid isPermaLink="false">http://www.omegaprojex.com/?p=167</guid>
		<description><![CDATA[So my boss where I work noticed that Google was showing 404 error&#8217;s in our Google webmaster tools, and that many of those 404&#8242;s are on invalid URL&#8217;s. With a site as big as ours, I wouldn&#8217;t say it would be uncommon to have other sites linking to our content and they mess up the [...]]]></description>
			<content:encoded><![CDATA[<p>So my boss where I work noticed that Google was showing 404 error&#8217;s in our Google webmaster tools, and that many of those 404&#8242;s are on invalid URL&#8217;s.  With a site as big as ours, I wouldn&#8217;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&#8242;s into working URL&#8217;s.  That being said, the problem URL&#8217;s this time around were ones that had characters on the end of the URL.  So for instance while our main page is &#8220;index.aspx&#8221;, we would have 404&#8242;s on &#8220;index.aspx;&#8221; or &#8220;index.aspx,&#8221;.  Obviously the problem is the funky characters on the end that shouldn&#8217;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</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="vbnet" style="font-family:monospace;">&lt;script runat<span style="color: #008000;">=</span><span style="color: #808080;">&quot;server&quot;</span>&gt;
<span style="color: #FF8000;">Private</span> <span style="color: #0600FF;">Sub</span> Application_OnBeginRequest<span style="color: #000000;">&#40;</span><span style="color: #FF8000;">ByVal</span> sender <span style="color: #FF8000;">As</span> <span style="color: #FF0000;">Object</span>, <span style="color: #FF8000;">ByVal</span> e <span style="color: #FF8000;">As</span> EventArgs<span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">Dim</span> application <span style="color: #FF8000;">As</span> HttpApplication <span style="color: #008000;">=</span> <span style="color: #0600FF;">CType</span><span style="color: #000000;">&#40;</span>sender, HttpApplication<span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">Dim</span> strRegex <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;\.aspx[;,()!^]+$&quot;</span>
        <span style="color: #0600FF;">Dim</span> RegexObj <span style="color: #FF8000;">As</span> System.<span style="color: #0000FF;">Text</span>.<span style="color: #0000FF;">RegularExpressions</span>.<span style="color: #0000FF;">Regex</span> <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> Regex<span style="color: #000000;">&#40;</span>strRegex<span style="color: #000000;">&#41;</span>
        <span style="color: #0600FF;">Dim</span> url <span style="color: #FF8000;">As</span> <span style="color: #FF8000;">String</span> <span style="color: #008000;">=</span> application.<span style="color: #0000FF;">Request</span>.<span style="color: #0000FF;">Url</span>.<span style="color: #0000FF;">AbsolutePath</span>
        <span style="color: #0600FF;">If</span> RegexObj.<span style="color: #0000FF;">IsMatch</span><span style="color: #000000;">&#40;</span>url<span style="color: #000000;">&#41;</span> <span style="color: #FF8000;">Then</span>
            url <span style="color: #008000;">=</span> Regex.<span style="color: #0600FF;">Replace</span><span style="color: #000000;">&#40;</span>url, strRegex, <span style="color: #808080;">&quot;.aspx&quot;</span> <span style="color: #000000;">&#41;</span>
            HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Response</span>.<span style="color: #0000FF;">Status</span> <span style="color: #008000;">=</span> <span style="color: #808080;">&quot;301 Moved Permanently&quot;</span>
            HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">Response</span>.<span style="color: #0000FF;">AddHeader</span><span style="color: #000000;">&#40;</span><span style="color: #808080;">&quot;Location&quot;</span>, url<span style="color: #000000;">&#41;</span>    
        <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">If</span>        
        HttpContext.<span style="color: #0000FF;">Current</span>.<span style="color: #0000FF;">RewritePath</span><span style="color: #000000;">&#40;</span>url<span style="color: #000000;">&#41;</span>        
    <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
&lt;<span style="color: #008000;">/</span>script&gt;</pre></td></tr></table></div>

<p>Now you would just put all of this into your Global.asax file.  The Global.asax (for those that don&#8217;t know) executes on every request, and therefore every url that is submitted to the site will execute through this function.</p>
<p>Basically this function will take the url passed in, and if it contains one or more of the following characters</p>
<pre style="background:#141414;font-size:14px;color:white;padding-left:3px;">;,()!^</pre>
<p>after a &#8220;.aspx&#8221; 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.</p>
<p>The function will also throw a 301 permanent redirect with a location of the fixed URL &#8211; this is for the search engines so that they know the page is not valid and where to go instead (basically for SEO).</p>
<p>In any case, this may not be the perfect solution (then again, I don&#8217;t know what else could be implemented&#8230;perhaps an ISAPI filter?), but it does cover most instances or bad URL&#8217;s linking to your site.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.omegaprojex.com/index.php/2008/11/13/fixing-404-errors-on-invalid-urls-with-aspnets-url-rewrite/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
