1

So I have an ActionName like so:

[ActionName("Chicago-Bears")]
public ActionResult ChicagoBears() {
  return View();
}

Google has this indexed as: http://www.example.com/Teams/ChicagoBears

I'm stuck using IIS6 and have no access to IIS myself.

Of course now, it has a hyphen in it. So, Google will show a 404 if someone clicks on that link.

How do I setup a 301 redirect in this instance? I can't create another method called ChicagoBears(), so...

Thanks guys.

AakashM
  • 62,551
  • 17
  • 151
  • 186
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
  • You don't need ChicagoBears, just another method with that ActionName Attribute. Then you can redirect in there. – Rangoric Dec 09 '10 at 18:13

4 Answers4

5

Create a route for Teams/ChicagoBears that points to an action that gives a permanent redirect.

In Global.asax...

routes.MapRoute("ChicagoBearsRedirect",
    "Teams/ChicagoBears",
    new { controller = "Teams", action = "RedirectChicagoBears" }
);

In TeamsController...

public ActionResult RedirectChicagoBears()
{
    return RedirectToActionPermanent("Chicago-Bears");
}
Brian
  • 37,399
  • 24
  • 94
  • 109
  • Gotta be a better way than that. Every link/route basically changed on the site. I wonder if I should just wait for Google to reindex as opposed to having this little redirect methods all over the place. – Jack Marchetti Dec 09 '10 at 18:01
  • 1
    Yuck. The route can accept Regular Expressions so if all of the routes should have a - in it you can just find the ones that don't and send them all to the same action that will do the redirect. Another possibility is to handle the 404 errors. – Brian Dec 09 '10 at 18:06
  • RedirectToActionPermanent is MVC 3. Honestly I don't have it installed yet either. It's a little more difficult to do 301s in MVC 2. – Brian Dec 09 '10 at 18:07
  • This was much easier than some of the other options. Thank you. – ctrlplusb Mar 23 '13 at 11:50
  • We can have this in separate controller check [this](http://stackoverflow.com/a/16980631/2218697) , hope helps. – Shaiju T Oct 12 '16 at 11:38
3

The URL Re-Write Module is your friend. Learn it, live it, love it...

http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

I used it extensively when I migrated from DasBlog to WordPress. All my old blog URLs are re-directed using 301's to the new ones. Highly recommended.

UPDATE: There are URL re-writers for IIS6. A quick google search turned up:

(Found via http://forums.iis.net/t/1160436.aspx.)

James Kovacs
  • 11,549
  • 40
  • 44
1

UPDATE: This blog I referenced seems to no longer be available so I updated the link to reference the internet archive version.

Check out this blog post for a great solution*: https://web.archive.org/web/20160528185929/http://www.eworldui.net/blog/post/2008/04/25/ASPNET-MVC-Legacy-Url-Routing.aspx

Essentially what he is doing is creating a reusable class that can be used for multiple routes, and they just issue a permanent redirect to the specified Action method.

**Note: This is not my blog, but one that I simply came across.*

mkchandler
  • 4,688
  • 3
  • 23
  • 25
0

Little late to the party on this one, but I wrote a blog post about permanent redirects for legacy routes that allows this -

routes.MapLegacyRoute(
    null, 
    "Teams/ChicagoBears", 
    new { controller = "Teams", action = "ChicagoBears", area="" }
);

The Location to redirect to is generated using the route values using Url.Action, so as long as you have a route in the RouteTable that matches the route values, the 301 redirect will work as intended. In your example, the generated URL should be http://www.example.com/Teams/Chicago-Bears when the URL pattern matches "Teams/ChicagoBears".

I won't repeat the code here as there's quite a bit and it's on the blog

Russ Cam
  • 124,184
  • 33
  • 204
  • 266