1

I rewrote my website from pure html file paths to MVC 4 extensionless urls.

Now I want to implement rule to redirect all requests to the old .html files to my home page.

In routeConfig.cs I tried this:

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

But, no matter what I try, I get a 404. I looks like MVC is processing .html files and ignoring my Route entry.

I also tried:

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

to tell MVC to not process .html files but still no luck.

Any ideas what I"m doing wrong?

Nico
  • 12,493
  • 5
  • 42
  • 62
IIS7 Rewrite
  • 779
  • 3
  • 16
  • 31
  • possible duplicate of [ASP.NET MVC Routing - add .html extension to routes](http://stackoverflow.com/questions/9331516/asp-net-mvc-routing-add-html-extension-to-routes) – Timothy Walters Jan 21 '14 at 05:27
  • MVC uses only .cshtml and .aspx pages. Why dont you try putting all your HTML content into a cshtml file – Rajshekar Reddy Jan 21 '14 at 05:41
  • I have done that. The problem is that old links that exist in google are getting 404's, so I need to redirect them to my home page. – IIS7 Rewrite Jan 21 '14 at 05:59

2 Answers2

2

Please make routes.RouteExistingFiles to true as per below code snippet.

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 }
        );

Also edit your web.config and add PageHandlerFactory-Integrated-HTML handlers to the list of handlers as per below code snippet.

<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>

Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
1

Since you're migrating from HTML to MVC, I think you shouldn't make the migration part of your application. Rather, use URL Rewrite module (URL Rewrite) to redirect requests.

You can use the IIS Management Console to configure the redirection rules, or edit web.config directly.

Here's a sample of a rule in web.config that can be relevant to your case:

<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>

It might not be the perfect rule for what you need (do some testing and tweaking), but I believe it is the way to do what you need. This way you'll be able to eventually get rid of the HTML redirection at some point and it won't be a part of your application; which also means that you don't need to recompile your application to make changes.

Yev
  • 276
  • 3
  • 13