How to block the Umbraco Cloud Url from live sites

Posted written by Paul Seal on December 13, 2024 Umbraco Cloud

I needed to block the default Umbraco Cloud on codeshare, because I don't want traffic going to that URL instead of codeshare.co.uk.

This post shows you what I did to get it working.

1. Add a simple Web.config file in the root of my site (in the same folder as the Program.cs file)

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Web.config file

The important part here is that I have got the basic config structure for the rewrite rules in there that I want to change.

2. Create a Web.Production.Config file which is used as a XDT Config Transform file by Umbraco Cloud.

You can have one for Web.Staging.Config and Web.Development.Config too if you like.

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
        <rewrite>
            <rules>
                <rule xdt:Transform="Insert" name="Redirects umbraco.io to actual domain" stopProcessing="true">
                    <match url=".*" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="\.umbraco\.io$" />
                        <add input="{HTTP_HOST}" pattern="^(dev-|stage-)(.*)?\.umbraco\.io$" ignoreCase="true" negate="true" />
                        <add input="{REQUEST_URI}" pattern="^/umbraco" ignoreCase="true" negate="true" />
                        <add input="{REQUEST_URI}" pattern="^/App_Plugins" ignoreCase="true" negate="true" />
                        <add input="{REQUEST_URI}" pattern="^/sb" negate="true" />
                        <!-- Don't redirect Smidge Bundle -->
                        <add input="{HTTP_COOKIE}" pattern="^(.+; )?UMB_UCONTEXT=([^;]*)(;.+)?$" negate="true" />
                        <!-- Ensure preview can render -->
                        <add input="{HTTP_HOST}" pattern="^localhost(:[0-9]+)?$" negate="true" />
                    </conditions>
                    <action type="Redirect" url="https://codeshare.co.uk/{R:0}" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

Web.Production.config

Don't forget to add the Web.config file

I copied this transform file from the official documentation, but I thought I would write a separate post so I can explain a bit more.

When I first did this, it failed when I pushed to production, because I didn't create the Web.config file and push that up with it too. So the default one that gets created for you didn't have the required elements to transform. That is why it is important to add that too.