Reputation: 1617
TL/DR: Can multiple rewrites (and a final redirect) for the same URI resource be chained over multiple web.config files living in subfolders along the URI's path during the same request?
I'm trying to migrate my classic ASP website from one shared virtual host to another; the new one using IIS7.5. My website has lots of subdomains, and the new hoster defaults to use one true website instance per subdomain, but my plan then only allows for a few of these subdomains, and since they're extremely low-traffic, one webserver instance should do perfectly fine. I therefore want the one main website instance just serving up all subdomains.
My path on their server is something like D:\websites\myaccount\mysite\wwwroot
. For cleanliness sake (to separate all files for the subdomains in their own folders) I've set up a single folder for each subdomain in my hosting root folder (so at e.g. D:\websites\myaccount\subdomain1
), and added virtual folders mapping names under the wwwroot to these subdomain folders (e.g. the virtual folder subdomain1
maps to D:\websites\myaccount\subdomain1
).
I then added rewrite rules to the main wwwroot folder's web.config to intercept requests for the subdomains and rewrite them to map to the virtual folders;
<rule name="Subdomain support" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="^(subdomain1|subdomain2|subdomain3)\.mysite\.com$" />
</conditions>
<action type="Rewrite" url="{C:1}/{REQUEST_URI}" />
</rule>
Using this, requests to http://subdomain1.mysite.com/dir1/file.asp
indeed do get served nicely via the subdomain1
virtual folder from D:\websites\myaccount\subdomain1\dir1\file.asp
.
I now want each subdomain to be able to add it's own rewrite rules to the mix. The most important one is that requests for the root of the subdomain should get mapped to a default file in the most appropriate language subfolder. Directly inside the subdomain1 folder I thus have another web.config file, holding:
<clear />
<rule name="subdomain root redirect to proper language subpath" stopProcessing="true">
<match url="^$" />
<conditions>
<add input="{HTTP_ACCEPT_LANGUAGE}" pattern="^(en|nl|de|es)" ignoreCase="true" />
</conditions>
<action type="Redirect" url="http://{HTTP_HOST}/{C:1}/index.asp" redirectType="Found" />
</rule>
<rule name="subdomain root redirect to default language subpath" stopProcessing="true">
<match url="^$" />
<action type="Redirect" url="http://{HTTP_HOST}/en/index.asp" redirectType="Found" />
</rule>
This should redirect the browser from subdomain1.mysite.com
to subdomain1.mysite.com/en/index.asp
(if your preferred browser language is set to "en.*").
However, I'm unable to get these rules to be picked up at all, no matter what I do. I've tested this by even matching .*
and changing the redirects to point to e.g. google.com, but the server just serves me a 403 forbidden, and when I add:
<directoryBrowse enabled="true" />
I'm getting served the folder listing of the subdomain's folder itself. My redirects are starting to scream "Do I mean nothing to you?!", and I want to stop the screaming.
However, when I type in subdomain1.mysite.com/subdomain1/
, the redirects do get picked up... I've also tried rewriting into regular (non-virtual) folders under the wwwroot and that exhibits the exact same symptoms.
Am I misguided in thinking this is possible? Could it be that since the original URI doesn't contain the (written-in) intermittant ...\subdomain1\...
part in the folder path that IIS doesn't take that folder's web.config into account?
My first attempt was to use .htaccess (.irwaccess) files, but I soon found out they do not inherit (right?), so that was my reason to look into web.config files.
Upvotes: 2
Views: 492
Reputation: 3494
Your requirement is redirect subdomain1.mysite.com to subdomain1.mysite.com/en/index.asp.
Then rewrite to subdomain1.mysite.com/subdomain1/en/index.asp right?
If you enable FRT, you will see that redirect rule under subdomain1 will never been executed. This isby design.
In this case, you should just move all these rules to the root folder's web.config and modify the order like this:
Mix rule in root level and application level is supported by IIS but I think your rules can't be separated in this case.
Upvotes: 2