Map subdomain to virtual directory Azure WebApps

旧时模样 提交于 2019-12-03 15:07:00

The answer posted by RuslanY will work (with some modifications to the rules slightly) however, after understanding more of the Azure portal and Web App configurations, it is not needed to host multiple sites within a single Web App (Its technically multiple web apps all sharing the resource plan you define, such as 2 instances of Standard Level 0 (S0))*.

As of today's Azure service offerings, the following is true. When you create a new Web App, you pecify the "App Service Plan" that the app falls into. If you have an App Service plan, lets say Standard with 2 instances, any Web App you deploy to that App Service plan shares those resources with other web apps in the same service plan, meaning you are not paying additional costs to host the additional web app if it is in the same App Service plan. I had assumed each web app was its own set of resources (it can be, but doesn't have to be). Given this, to accomplish what I need, I simply create a web app for each sub domain and place them all into the same App Service plan. I now am hosting multiple sites, not paying for 2 servers per site (what I wanted to avoid) and I don't have to use URL rewrites or HTTP Handlers.

I hope this write-up helps others understand the structure of the Azure Web Apps a little bit better. The current online documentation, from what I can tell, doesn't make this exactly clear.

This may be possible to do with URL rewrite rule which takes the hostname of the request and rewrites the request URL to start with the subdomain extracted from the hostname:

 <system.webServer>
    <rewrite>
        <rules>
            <rule name="Rewrite Subdomain To Directory">
                <match url=".*" />
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^www\.mydomain\.com$" negate="true" />
                    <add input="{HTTP_HOST}" pattern="^(.+)\.mydomain.\com$" />
                </conditions>
                <action type="Rewrite" url="{C:1}/{R:0}" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>

To understand the syntax of the rewrite rules you can refer to the IIS.net documentation about URL rewrite module.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!