简体   繁体   English

RedirectPermanent-MVC和短网址

[英]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. 例如,www.domain / Appointment / RequestAppointment调用约会控制器和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. 市场营销人员经常要求我为较短版本的URL构建重定向,以用于社交媒体或广告牌。

So, they would like to see www.domain/Appointment/RequestAppointment as www.domain/appointment . 因此,他们希望将www.domain / Appointment / RequestAppointment视为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. 现在,我们的SEO人员告诉我新的URL(www.domain / appointment)返回302重定向(据我了解),这对SEO不利。 They would like a 301. 他们想要301。

So, I understand I can return the (desired from SEO viewpoint) 301 redirect by using: 因此,我知道我可以通过使用以下命令返回(从SEO观点出发)301重定向:

//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 . 我还必须对大约10个其他唯一的URL做同样的事情,这些URL属于www.domain / controller / action / id格式,将被重定向到www.domain / promotion

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. 您可以将路由引擎设置为自动将给定的URL映射到特定的控制器操作,即使它与给定的默认结构不匹配也是如此。 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. 您可以根据需要添加任意多个,它们将返回200,甚至不返回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. 请记住,完成此操作后,如果要执行索引操作,则需要将URL设为“约会/索引”。 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: 但是,您可以使用以下两个URL进入同一页面,而不会发生服务器或客户端重定向:

www.domain.com/appointment www.domain.com/appointment

www.domain.com/appointment/requestappointment www.domain.com/appointment/requestappointment

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM