I have a database driven ASP.NET site, which uses a single APSX page to display all site pages. So all the URLs of the site are in the following format:
/main.aspx?page=Page+Title+One
/main.aspx?page=Another+Article+Title
Unfortunately Google has indexed many non-existent URLs, mostly cutting part of the query string. For example:
/main.aspx?page=Page+Title or /main.aspx?page=Page
instead of the correct one:
/main.aspx?page=Page+Title+One (this is indexed by Google as well of course).
What I'm trying to achieve is, if the query string page name doesn't have a matching entry in my DB, to force a 404 error server side, first for better user experience and second to tell Google that these pages don't exist.
Here is my code and is not working. I mean it just displays an empty page in IE 8, and error in the latest Firefox:
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.StatusCode = 404;
System.Web.HttpContext.Current.Response.Status = "404 Not Found";
System.Web.HttpContext.Current.Response.End();
I see the proper 404 code in my logs however I don't get redirected to my error page specified in my web.config:
<customErrors mode="On">
<error statusCode="404" redirect="404.aspx"/>
</customErrors>
What am I doing wrong?
Another thing that worries me a bit is if I successfully force 404 error on any query string entries that don't have a match in my db, wouldn't that affect the main.aspx page itself effectively affecting all my existing pages as well? Here is what I mean:
I 404 main.aspx?page=Non-Existing-Title, however my 404 affects main.aspx with any query strings including valid ones, for example main.aspx?page=Existing-Title.
The site runs on IIS6/Win 2003.
Thanks for your help!
John