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?