Notice: Only variables should be assigned by reference in G:\Web\AzureTechGuy\plugins\content\jhimagepopup\jhimagepopup.php on line 90 Notice: Undefined property: stdClass::$introtext in G:\Web\AzureTechGuy\plugins\content\jhimagepopup\jhimagepopup.php on line 108

Notice: Only variables should be assigned by reference in G:\Web\AzureTechGuy\plugins\content\jhimagepopup\jhimagepopup.php on line 90

The default App Service domain xxx.azurewebsites.net cannot be changed or removed. It is there to stay. If you don't want users to access it, your option is to redirect the azurewebsites.net requests to your custom domain. 

If your Web App is running on Windows then you can achieve this with a web.config placed in the wwwroot directory.

Table of Contents

Redirect azurewebsites.net

There place the following code under <configuration>/<system.webServer>/<rewrite>/<rules>. Change the CAPITALZED webappname and custom domain below with your own values.

<rule name="Redirect azurewebsites.net to Custom Domain" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^WEBAPPNAME.azurewebsites.net$" />
          </conditions>
          <action type="Redirect" url="http://CUSTOMDOMAIN.COM/{R:0}" appendQueryString="true" redirectType="Permanent" />
        </rule>

This is the complete web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect azurewebsites.net to Custom Domain" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^WEBAPPNAME.azurewebsites.net$" />
          </conditions>
          <action type="Redirect" url="http://CUSTOMDOMAIN.COM/{R:0}" appendQueryString="true" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Redirect azurewebsites.net + www

If you want to not only redirect from azurewebsites.net to your custom domain but then to www. use the below web.config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect azurewebsites.net to Custom Domain" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^WEBAPPNAME.azurewebsites.net$" />
          </conditions>
          <action type="Redirect" url="http://CUSTOMDOMAIN.COM/{R:0}" appendQueryString="true" redirectType="Permanent" />
        </rule>
        <rule name="Redirect to WWW" enabled="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" negate="true" pattern="^www\." />
          </conditions>
          <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" appendQueryString="true" redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Tip!

If these rules give you a result of "Too many redirects" check if you have some other service before your Web App, such as Application Gateway or Azure Front Door. Another reason could be a redirect rule from outside of the web.config. Such as HTTPS Only on your Web App TLS/SSL Settings or a redirection coming from your applicaiton code which causes additional redirects 

Demo

Try this demo. Open the link below with F12 Dev tools opened under Networking tab and observe how fracefully the redirection happens:

  • First request hits and is being redirected (301) to the custom domain
  • Second requests hits and is being redirected (301) to www
  • Third request hits the proper destination with (200) OK

https://tek-labnet.azurewebsites.net

Redirect to azurewebsites.net in Linux

In Apache you can achieve this with putting a redirect rule in the .htaccess file. Try the following:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^WEBAPPNAME.azurewebsites.net$ [NC]
RewriteRule ^(.*)$ https://CUSTOMDOMAIN.COM/$1 [L,R=301]