0

I have got a single IIS website that will host multiple sites (using a CMS). Each site has its own domain and will have its own theme, so I want the error pages (404 and 500) to be specific to each site as well.

Normally I would set up the error pages using the httpErrors section in the web.config, but I think that only works for a single set of error pages?

The idea I had in my head was to use the URL Rewrite module to rewrite the static file URL to my site-specific file, but that doesn't seem to work:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" />
  <error statusCode="404" path="/404.html" responseMode="ExecuteURL" />
  <remove statusCode="500" />
  <error statusCode="500" path="/500.html" responseMode="ExecuteURL" />
</httpErrors>

And the rewrite rules:

<rule name="Rewrite Error Pages - Site 1" enabled="true" stopProcessing="true">
  <match url="^(\d{3}).html$" />
  <conditions logicalGrouping="MatchAny">
      <add input="{HTTP_HOST}" pattern="^mysite1.com$" />
  </conditions>
  <action type="Rewrite" url="{R:1}-mysite1.html" />
</rule>
<rule name="Rewrite Error Pages - Site 2" enabled="true" stopProcessing="true">
  <match url="^(\d{3}).html$" />
  <conditions logicalGrouping="MatchAny">
      <add input="{HTTP_HOST}" pattern="^mysite2.com$" />
  </conditions>
  <action type="Rewrite" url="{R:1}-mysite2.html" />
</rule>

So I have a few static HTML files for each site as well:

  • 404-mysite1.html
  • 500-mysite1.html
  • 404-mysite2.html
  • 500-mysite2.html

The rewrite rules work fine when I go to http://mysite1.com/404.html for example, as it will rewrite it correctly and return the content of 404-site1.html.

But when I go to a non-existing URL (e.g. http://mysite1.com/foo) it will return a blank page. I can see in the request trace (enabled through Failed Request Tracing in IIS) that it correctly tries to request /404.html (as is in my httpErrors section), but it won't then rewrite it to /404-site1.html.

Does anyone know if this is even possible? Or are there other ways to set up static HTML error pages for different domains in the same IIS site?

1 Answers1

0

In the end I figured it's not possible using URL Rewrite rules, so am now using a simple .aspx page that will check the host name and render the correct error page content.

So I removed the URL Rewrite rules mentioned in the question, and change the httpErrors to this:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404" />
  <error statusCode="404" path="/404.aspx" responseMode="ExecuteURL" />
  <remove statusCode="500" />
  <error statusCode="500" path="/500.aspx" responseMode="ExecuteURL" />
</httpErrors>

And in the 404.aspx file I do the following:

<%@ page trace="false" validateRequest="false" %>

<%-- set correct site name based on request domain --%>
<% string siteName = ""; %>
<% string hostName = Request.Url.Host; %>

<% if (hostName == "mysite1.com") { siteName = "mysite1"; } %>
<% if (hostName == "mysite2.com") { siteName = "mysite2"; } %>

<%-- return file content with a 404 status code --%>
<% Response.StatusCode = 404; %>
<% if (!string.IsNullOrEmpty(siteName)) { Response.WriteFile("404-" + siteName + ".html"); } %>

This seems to work fine for what I want it to do.