Subdirectory and subdomain pointers with URL rewrite

Overview

Barring conflicting rewrite rules, routing, or a site built on the ASP.NET Core Framework, URL rewrite may be used to redirect domains and subdomains to subdirectories on an existing site.

There are two parts to this. First you add the domain pointer or the subdomain in the Control Panel, so that the request reaches your site at all. Then you add a rewrite rule to the web.config in the document root of the site, so that the request is served from the right subdirectory.

Syntax examples for each type of redirection are below. For ASP.NET Core, see the last section.

Finding domain pointers and subdomains in the Control Panel

  1. Log in to the Everleap Control Panel at cp.everleap.com.
  2. From the top menu click Shared Sites.
  3. On the Shared Cloud Sites Overview page, click Manage next to the applicable site.
  4. On the Site Overview page, look at the General Information panel. It has a Domain Pointers row and a Subdomains row, each with its own Manage link.
The General Information panel on the Site Overview page, showing the Domain Pointers and Subdomains rows each with a Manage link

Adding a Domain Pointer

  1. In the General Information panel, click Manage on the Domain Pointers row.
  2. On the Domain Pointers page, under Add Domain Pointer, type the domain into the Domain Name field.
  3. Tick Enable Email Alias if you also want mail for that domain delivered to this site's mailboxes.
  4. Click Add Domain Pointer.
The Domain Pointers page showing the existing pointer list and the Add Domain Pointer form with its Domain Name field, Enable Email Alias checkbox and Add Domain Pointer button

Redirecting a domain to a subdirectory

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="domain.com" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^(www.)?domain.com" />
            <add input="{PATH_INFO}" pattern="^/subdirectory/" negate="true" />
          </conditions>
          <action type="Rewrite" url="\subdirectory\{R:0}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

In the above syntax example, "domain.com" on lines 6 and 9 should be replaced by the domain pointer, and "subdirectory" on lines 10 and 12 should be replaced by the subdirectory where the files have been uploaded.

Adding a Subdomain

  1. In the General Information panel, click Manage on the Subdomains row.
  2. On the Subdomains page, under Add Subdomain, type the name into the Subdomain field.
  3. Click Add Subdomain.
The Subdomains page showing the existing subdomain list and the Add Subdomain form with its Subdomain field and Add Subdomain button

Redirecting a subdomain to a subdirectory

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="subdomain.domain.com" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^subdomain.domain.com$" />
            <add input="{PATH_INFO}" pattern="^/subdirectory/" negate="true" />
          </conditions>
          <action type="Rewrite" url="\subdirectory\{R:0}" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

In the above syntax example, "subdomain.domain.com" on lines 6 and 9 should be replaced by the applicable subdomain, and "subdirectory" on lines 10 and 12 should be replaced by the subdirectory where the files have been uploaded.

Verify it worked

Browse to the domain pointer or subdomain. You should see the content from the subdirectory, and the address bar should still show the pointer or subdomain rather than the subdirectory path.

If after adding either rule the subdirectory is being appended to the URL, verify that line 10 in the above syntax has been added to remove it.

ASP.NET Inheritance

Note that if the subdirectory is set as an application starting point and there is an ASP.NET application in the parent directory, the child application will inherit settings from the parent application. For more information about disabling inheritance, see https://stackoverflow.com/a/5968658.

ASP.NET Core

Microsoft.AspNetCore.Rewrite is middleware with support for URL rewrite rules. For an overview on using the middleware, see the microsoft.com article URL Rewriting Middleware in ASP.NET Core. To use URL rewrite rules like those above through a referenced xml file, see the IIS URL Rewrite Module rules section of that article.

Related Articles