FreeMars
FreeMars

Reputation: 145

RedirectPermanent - MVC and short(er) URL

I'm using default route configuration of domain/controller/action.

For instance, www.domain/Appointment/RequestAppointment calls the Appointment controller and RequestAppointment ActionResult.

I've been often asked by our Marketing folks to build redirects for shorter versions of our URLs for use in social media or on billboards.

So, they would like to see www.domain/Appointment/RequestAppointment as www.domain/appointment.

I don't have an issue doing that, in this case I am just:

//Appointment Controller
public ActionResult Index()
    {
        return RedirectToAction("RequestAppointment");
    }

Now, our SEO person is telling me the new URL (www.domain/appointment) is returning a 302 redirect (which I understand) and that is bad for SEO. They would like a 301.

So, I understand I can return the (desired from SEO viewpoint) 301 redirect by using:

//Appointment Controller
public ActionResult Index()
    {
        return RedirectPermanent("http://www.domain.com/Appointment/RequestAppointment");

    }

It works, but is there a better way or a best practice to handle this? I figure it can't be that uncommon, but I can't find anything.

I will also have to do the same thing with about 10 other unique URLs that fall into the www.domain/controller/action/id format that would be redirected to www.domain/promotion.

Upvotes: 1

Views: 787

Answers (1)

krillgar
krillgar

Reputation: 12815

You don't need to set up redirects for these. You can set up the Routing engine to automatically map a given URL to a specific controller action, even if it doesn't match the given default structure. Keep in mind, these need to be BEFORE the default route. You can add as many of these as you want, and they will return a 200, not even a 301.

routes.MapRoute("Request Appointment",
                "appointment", // <= What you want the URL after "www.domain" to be
                new { controller = "Appointment", action = "RequestAppointment" });

Bear in mind, with this in place, you would need to have the URL be "appointment/index" if you want to get to the index action. However, the same holds true with any other sort of mapping. But, you can get to the same page with both of the following URLs with no server or client redirects occurring:

www.domain.com/appointment

www.domain.com/appointment/requestappointment

Upvotes: 2

Related Questions