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
- Using Azure Portal, open a new SSH session. (https://<default domain>.scm.azurewebsites.net/webssh/host)
- Type cd
/etc/nginx/conf.d/default.conf
- Type
vim /etc/nginx/conf.d/default.conf
- 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.
Leave a Reply