【问题标题】:Redirect requests from an old website in MVC在 MVC 中重定向来自旧网站的请求
【发布时间】:2013-04-06 20:06:19
【问题描述】:

我使用 ASP.Net MVC 3 为客户构建了一个网站,该网站旨在替换一个我没有构建并用 PHP 编写的旧网站。

我在新网站上的大部分页面都映射到原来的旧页面,例如 www.mysite.com/contactus 曾经是 www.mysite.com/contactus.php

在查看了我的错误日志(由 Elmah 记录)后,我收到一些旧页面请求的错误,如下所示:

路径“/ContactUs.php”的控制器未找到或未实现 IController。

是否有人对如何纠正此问题提出建议,理想情况下将用户重定向到新目的地(如果存在),或者只是将其默认为主页。

【问题讨论】:

    标签: c# asp.net-mvc redirect


    【解决方案1】:

    您应该能够通过 web.config 中的 IIS 重写规则来执行此操作:

    <rewrite>
      <rules>
        <rule name="Remove .php suffix">
          <match url="^(.*).php$" />
          <action type="Rewrite" url="{R:1}" />
        </rule>
      </rules>
    </rewrite>
    

    这应该去掉任何传入请求的“.php”后缀。更多信息请看这里:http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module

    【讨论】:

      【解决方案2】:

      你可以使用这条路线:

      routes.MapRoute(
          name: "oldphp",
          url: "{*path}",
          defaults: new { controller = "PhpRedirect", action="Get" },
          constraints: new { path = @".*\.php" });
      

      然后像这样实现PhpRedirectController

      public class PhpRedirectController : Controller
      {
          [HttpGet]
          public ActionResult Get(string path)
          {
              // TryGetNewUrl should be implemented to perform the
              // mapping, or return null is there is none.
              string newUrl = TryGetNewUrl(path);
              if (newUrl == null)
              {
                  // 404 not found
                  return new HttpNotFoundResult();
              }
              else
              {
                  // 301 moved permanently
                  return RedirectPermanent(newUrl);
              }
          }
      }
      

      【讨论】:

      • @AntP:我已经更新了我的答案,以便它也适用于这种情况。
      猜你喜欢
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 2015-04-17
      • 1970-01-01
      • 2010-11-17
      相关资源
      最近更新 更多