Matthew Nilsen

Software Engineer, Videographer, Photographer, Mead-maker

How to Redirect Default *.azurewebsites.net Domain to Custom Domain on WordPress

Recently, I realized that my new wordpress.org website wasn’t being indexed properly by Google. Search Console gave me the following error:

Page is not indexed: Duplicate, Google chose different canonical than user

The first solution I found was to go into wp-config.php and set the WP_HOME and WP_SITEURL constants to your custom domain. However, I noticed a comment that indicated that probably wasn’t the correct way to do this:

I figured that if I did this, App Service deployment slots wouldn’t work properly. Then, I found a new solution titled “Prevent Azure Default Domains being Indexed in Search Engines” which sounded much more like what I was looking for. For that solution, Method 2 involved adding a url rewrite rule which would add a noindex header to tell the search bots not to index your default *.azurewebsites.net domain. Unfortunately, it wasn’t written for linux and didn’t have very descriptive instructions for which file to edit or where to find it.

Finally, I finally found a solution titled “Redirect azurewebsites.net to your custom domain.” This solution is written specifically for App Service Linux running on Nginx, which is what the default “WordPress on App Service” configuration uses. After some further verification, it appears this solution is officially endorsed my Microsoft: https://learn.microsoft.com/en-us/answers/questions/1338232/xxx-azurewebsites-net-doesnt-redirect-to-custom-do

Below, you’ll find the updated version of the aforementioned solution.

The Solution

  1. Using Azure Portal, open a new SSH session. (https://<default domain>.scm.azurewebsites.net/webssh/host)
  2. Type cd /etc/nginx/conf.d/default.conf
  3. Type vim /etc/nginx/conf.d/default.conf
  4. Make the following modifications:

Replace server_name _; with server_name <domain>;

E.g.

Add the following block of code to this existing location block:

if ( $host != $server_name ) {
    return 301 $scheme://<domain>$request_uri;
}

E.g.

Results

Now, traffic will successfully be redirected from your default domain to your custom domain! This means that Google should now be able to select the correct URL to index, causing your custom domain to appear in Google search results.

One response to “How to Redirect Default *.azurewebsites.net Domain to Custom Domain on WordPress”

  1. Benjamin Avatar

    you forgot to say that at the end you need to run “nginx -s reload” otherwise the changes won’t be taken.

Leave a Reply

Your email address will not be published. Required fields are marked *