Redirecting non-WWW to WWW in an Azure Website
add this code this code under <system.webServer>
section
<rewrite><rules><rule name="Redirect to www"> <match url=".*" /> <conditions logicalGrouping="MatchAny"> <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" /> </conditions> <action type="Redirect" url="http://www.{HTTP_HOST}/{R:0}" redirectType="Permanent"/></rule></rules></rewrite>
If you want a REAL redirection (i.e. when a user types example.com
then the address in the browser automatically changes to www.example.com
) then you have two options:
- Using the forwarding feature offered by GoDaddy (you can find it in the GoDaddy dashboard (domain details page). In this way you can point
example.com
to a GoDaddy IP that responds with a redirection towww.example.com
- Write some code in ASP.NET that detects when the address is missing "www." and then redirecting to
www.example.com
However, if you just want the users that type example.com
to get the same content as users typing www.example.com
and you don't mind people seeing example.com
without www in their address bar, then proceed as following:
- Get the virtual IP address associated to your Azure website: from the Azure management portal click on your website, go to the dashboard section and click Manage Domains. You should get something like "The IP address to use when you configure A records: xxx.xxx.xxx.xxx".
- Go to GoDaddy and set an A record with "Host" to
@
and "Points to" set to the IP found at step 1 - Add a CNAME record with "Host" set to
awverify
and "Points to" set to the address of your azure website prefixed withawverify
(for exampleawverify.mywebsite.azurewebsites.net
) - Add a CNAME record with "Host" set to
www
and "Points to" set to the address of your azure website (for examplemywebsite.azurewebsites.net
) - Save the zone file in GoDaddy
- Go back to windows azure in the "Manage Domains" section of your website and add both
example.com
andwww.example.com
to the list of domain names.
If you get any error at step 6, just wait some hours to let the DNS changes to propagate and retry.
More info here: https://www.windowsazure.com/en-us/documentation/articles/web-sites-custom-domain-name/
Instead of a generic MatchAll with negate=true, I prefer to catch only the naked domain and redirect if it is a match. This works well for Azure since I am only interested in a single domain and then I don't have to write a bunch of exclusions for localhost, localtest.me, subdomains, etc.
Here is the rule... just change example
in the pattern to your domain:
<system.webServer> <rewrite> <rules> <rule name="Redirect To WWW" enabled="true" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAny"> <add input="{HTTP_HOST}" pattern="^example\.com$" /> </conditions> <action type="Redirect" url="http://www.{HTTP_HOST}{URL}" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer>