themightylc
themightylc

Reputation: 324

Rewriting subdomain to directory with web.config on Azure Web App

I am trying to host all my subdomains in respective folders on my webserver meaning

https://foo.bar.com/index.html

is located at

https://bar.com/foo/index.html

by my current configuration the file cannot and should not be accessed via the "original" link, though.

My current approach works, except with urls, that point to a subdirectory without a filename in the path - in that case the subdomain is re-included in the path of the rewritten url:

https://foo.bar.com/this/does/work.html

serves the file located at

https://bar.com/foo/this/does/work.html

while

https://foo.bar.com/this/does/not/work

is rewritten in the browser to

https://foo.bar.com/foo/this/does/not/work

which for obvious reasons results in a 404.

If you however enter the url with the issues directly in the browser

https://foo.bar.com/this/does/not/work

it works as expected.

I know I'm using the word "obviously" in a loose sense. Here's a quick demo:

https://egal.todoservice.app/

as you should see there, the link on the page to https://egal.todoservice.app/sub/dir/

takes you to https://egal.todoservice.app/egal/sub/dir/

but try to enter that link directly in your address bar and it works - it does for me at least, tested in Chrome, Edge and Firefox.

UPDATE: strangely, using the subdir link here on stackoverflow works as expected...

This is the part of my web.config that is responsible for the subdomain-rewrite

<rule name="Rewrite Subdomain To Directory" stopProcessing="false">
    <match url=".*" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^(.+)\.todoservice\.app$" />
    </conditions>
    <action type="Rewrite" url="{C:1}/{R:0}" />
</rule>

And here is my full web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <staticContent>
             <mimeMap fileExtension="." mimeType="text/plain" />
             <mimeMap fileExtension=".nupkg" mimeType="application/zip" />
         </staticContent>        
        <rewrite>
            <rules>
                <rule name="Rewrite SMZC URL" stopProcessing="false">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="www.sadmenszombieclub.com"  />
                    </conditions>
                    <action type="Rewrite" url="smzc/{R:0}" />
                </rule>
                <rule name="Rewrite imagiro URL" stopProcessing="false">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="www.imagiro.net"  />
                    </conditions>
                    <action type="Rewrite" url="imagiro/{R:0}" />
                </rule>

                <rule name="Rewrite Subdomain To Directory" stopProcessing="false">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^(.+)\.todoservice\.app$" />
                    </conditions>
                    <action type="Rewrite" url="{C:1}/{R:0}" />
                </rule>

                <rule name="Rewrite root path to service Directory" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^todoservice\.app$" />
                    </conditions>
                    <action type="Rewrite" url="service/{R:0}" />
                </rule>                
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

The whole site is hosted as an Azure Web App.

Upvotes: 3

Views: 1076

Answers (1)

Lex Li
Lex Li

Reputation: 63298

Copied from comment.

You are doing more than simple rewriting, but reverse proxying. Thus, a few changes are needed to be carried out,

  1. In your action, instead of rewriting the actual path (<action type="Rewrite" url="{C:1}/{R:0}" />), you should also append the full domain name to url. In that way IIS knows you want to set up a reverse proxy.
  2. Enable ARR proxy mode in Azure App Service by following blog posts such as https://tomssl.com/2015/06/15/create-your-own-free-reverse-proxy-with-azure-web-apps/

Upvotes: 2

Related Questions