0

Okay, here's the deal:

I have an aspx page that looks like mysite.com/UnitDetails.aspx?UnitID=123

Right now I've setup my routing to be able to take in something like mysite.com/My/Path/123 and redirect it to UnitDetails.aspx. This works great.

However, what I'd like to be able to also do is redirect the user to the clean url if they type in the aspx page. For example, if you have mysite.com/UnitDetails.aspx?UnitID=123 bookmarked, I'd like it to show up as mysite.com/My/Path/123.

How can I accomplish this? Here's what I have right now in my routing:

        RouteTable.Routes.MapPageRoute("unit_details", "{area}/{property}/{unit_id}", "~/UnitDetails.aspx")
Austin
  • 387
  • 6
  • 11

1 Answers1

0

Okay, I figured it out!

Just needed to add this into my UnitDetails.aspx code behind in the Page_Load:

        //issue a 301 if legacy route requested to boost SEO
    if (Request.Url.AbsolutePath.Contains("UnitDetails.aspx"))
    {
        var unit = BeachGuide.Models.Unit.GetById(Convert.ToInt32(Request.Params["UnitID"]));
        Response.RedirectPermanent(unit.relative_url);
    }

So now if they request the new url, they won't get caught in an infinite loop. If they request the old url, we have a base case that redirects them to the new url via a 301 redirect. This will help search engines know to use the new route as the canonical url.

Austin
  • 387
  • 6
  • 11