【问题标题】:How to redirect all requests to html files如何将所有请求重定向到 html 文件
【发布时间】:2026-02-11 20:10:01
【问题描述】:

我将我的网站从纯 html 文件路径改写为 MVC 4 无扩展 url。

现在我想实施规则,将所有对旧 .html 文件的请求重定向到我的主页。

routeConfig.cs 我试过这个:

        routes.MapRoute(
            name: "Redirects",
            url: "posts/{page}.html",
            defaults: new { controller = "Home", action = "Index" }
        );

但是,无论我尝试什么,我都会得到 404。我看起来 MVC 正在处理 .html 文件并忽略了我的 Route 条目。

我也试过了:

routes.IgnoreRoute("posts/{file}.html");

告诉 MVC 不要处理 .html 文件,但仍然没有运气。

任何想法我做错了什么?

【问题讨论】:

  • MVC 仅使用 .cshtml 和 .aspx 页面。为什么不尝试将所有 HTML 内容放入 cshtml 文件中
  • 我已经做到了。问题是 google 中存在的旧链接正在获取 404,所以我需要将它们重定向到我的主页。

标签: asp.net-mvc asp.net-mvc-4 redirect url-redirection


【解决方案1】:

请按照以下代码 sn-p 将 routes.RouteExistingFiles 设置为 true。

routes.RouteExistingFiles = true;

        routes.MapRoute(name: "Redirects",
            url: "HtmlPage.html",
            defaults: new { controller = "Home", action = "Html", page = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

同时编辑您的 web.config 并将 PageHandlerFactory-Integrated-HTML 处理程序添加到处理程序列表中,按照以下代码 sn-p。

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  <add name="PageHandlerFactory-Integrated-HTML" path="*.html"
           verb="GET,HEAD,POST,DEBUG" type="System.Web.UI.PageHandlerFactory"
           resourceType="Unspecified" preCondition="integratedMode" />
</handlers>

【讨论】:

    【解决方案2】:

    既然您正在从 HTML 迁移到 MVC,我认为您不应该将迁移作为应用程序的一部分。 而是使用 URL 重写模块 (URL Rewrite) 来重定向请求。

    您可以使用 IIS 管理控制台配置重定向规则,或直接编辑 web.config。

    以下是 web.config 中可能与您的案例相关的规则示例:

    <configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Html to MVC" stopProcessing="true">
                    <match url=".*/(.*).html" />
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" />
                    </conditions>
                    <action type="Redirect" url="/Index/{R:1}" redirectType="Found" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
    </configuration>
    

    它可能不是你需要的完美规则(做一些测试和调整),但我相信这是做你需要的方法。这样,您最终将能够在某个时候摆脱 HTML 重定向,并且它不会成为您应用程序的一部分;这也意味着您无需重新编译应用程序即可进行更改。

    【讨论】: