1

I am converting an existing classic ASP website to (VB) MVC and I don't want any of the existing URLs to break. I have read many posts (like this one: Routing Classic ASP Requests To .NET - SEO Redirects) about how to do the proper 301 redirect. With the latest MVC release, I've gathered that

Response.RedirectPermanent(objRedirect.new_url, True)

is all that is needed.

I have entered all of my old URLs in a database table with a corresponding column of the new URL. I have added code in my custom 404 page to get the original URL:

Dim strURL As String = Request.RawUrl.Substring(Request.RawUrl.IndexOf("aspxerrorpath=") + 15).ToLower()

so I can look it up in the database. (Interesting sidenote, MSDN's documentation here - Redirect Mode - seems to say that if I set RedirectMode=ResponseRewrite in the CustomErrors section of my web.config, I won't have to worry about doing the above, but when I've tried that, I get IIS errors saying it won't serve an ASP page?!?!?)

The problem I am encountering is that any of my old, Classic ASP URLs that have the same directory as a new MVC route are somehow being partially routed. For example, "/test/default.asp" shows up as "/test/test" in the above strURL variable of my error page.

I do have a route setup for "test":

routes.MapRoute("Test", _
                "test/{action}", _
                New With {.controller = "test", .action = "index"})

in Global.aspx.vb, but I also have tried every conceivable way to ignore all ASP pages in routes (seemingly to no avail). Here are the attempts I've made (I did see one old - 2008 - post by Phil Haack that said I could only have one of these "catch all" ignore routes, but don't know if that's still valid or not?):

routes.IgnoreRoute("{file}.asp")
routes.Ignore("{resource}.asp/{*pathInfo}")
routes.IgnoreRoute("{resource}.asp/{*pathInfo}")
routes.Add(New Route("{resource}.asp/{*pathInfo}", New StopRoutingHandler()))

and none of them seemed to make any difference (I tried them all one at a time obviously).

This isn't isolated to just one route either - it occurs for any directories that existed on the old site that match a named route on the new site.

Thanks in advance for any and all suggestions you have!

UPDATE: Here are 2 more "issues" I've discovered:

  1. I am loosing the original querystring. So, if product.asp?id=1 is requested, all I have in the error page is product.asp (any idea how to get at/save the original querystring?)
  2. Another one of my routes looks like this:

    routes.MapRoute("IndependentSales", _ "independentsales/{action}", _ New With {.controller = "independentsales", .action = "index"})

and when I request "/resale/default.asp", it goes to "resale/independentsales". WHAT is up with that???

Community
  • 1
  • 1
WebDave
  • 55
  • 1
  • 9

2 Answers2

0

Have you tried to use web.config?

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Remove index.php" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="^(.*)$" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="index.php/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

The example above is used to rewrite a CodeIgniter app to omit the index.php file from the URL. You can do many things with it, considering the conditions part. In this case, I only do the rewriting if it's not a file and not a directory.

Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69
  • I think you're referring to Microsoft's URLRewriter tool as [explained by Scott Guthrie](http://weblogs.asp.net/scottgu/archive/2010/04/20/tip-trick-fix-common-seo-problems-using-the-url-rewrite-extension.aspx)? I did see that, but since our site is hosted by an ISP, I wasn't sure I'd be able to install that so I'm trying to not have to go there. – WebDave Dec 18 '11 at 00:48
  • I can't confirm that but any host that suports ASP.Net suports the web.config file. You can create it in your machine (either by hand or using IIS) and upload it to the app root. Its a standard feature of IIS 7 and above. – Ricardo Souza Dec 18 '11 at 00:54
  • I did add the rewrite code to my web.config, but it didn't do anything. I'm pretty sure you have to have the URLRewrite code installed within IIS for it to work. – WebDave Dec 18 '11 at 06:50
  • You are right. But I thought it was a standard features of IIS 7+ – Ricardo Souza Dec 18 '11 at 13:25
0

OK, after hours and hours of reading/researching/debugging, I came to the conclusion that I wasn't doing anything wrong and that there was something "buggy" in Visual Studio, .NET, MVC ... something! And I was right! I created a new project from scratch, copied all my code over there and everything worked as it should!

(Well, I'm not sure "as it should" is correct, but I did get my code to function the way I wanted. Requests for ASP pages still hit the MvcApplication_BeginRequest - which I don't think they should with my IgnoreRoute entries???? - but at least the extension was still there when it got there, so I could look-up the ASP page in my database and do the redirect!)

So, this may have been unique to me, but maybe I can save others hours of frustration ... if you think you have everything correct, maybe you do! Create a new project and try that!

... It sucks to be a programmer! ;-D

(Musings ... The 3 things that I didn't add back into my new project were:

  1. the logging tool ELMAH
  2. the RouteMagic DLL
  3. and the RouteDebugger.dll.

I may try subtracting them from my original project and see how that goes. Given the popularity of ELMAH and who wrote the route ones, I can't imagine that there would be a problem with them, but who knows ...?)

WebDave
  • 55
  • 1
  • 9
  • One more "aha moment" (or maybe "duh! moment"!?!?!) is that when you are testing permanent redirects and get them wrong the first time, your browser "remembers" that and every future test of that URL doesn't go through your code, but rather your browser just redirects it (... it didn't occur to me why I wasn't hitting my code breakpoints!) – WebDave Jan 01 '12 at 23:28