mydomain.com works fine.
www.mydomain.com gives 404 - File or directory not found.

I don't want people to use www.mydomain.com instead of mydomain.com, but I also don't want the 404 error in their face if the do.

The A record at GoDaddy looks like:
Host - Points to - TTL
@ - ip address - 600 seconds

How to fix the issue so that if user types in www.mydomain.com they would be magically redirected to mydomain.com?


The solution to the problem was to have both domain names (mysite.com and www.mysite.com) explicitly set up in Azure as custom domains.

Then to use URL rewrite module in IIS (already installed in Azure) by adding rewrite section in the Web.config file:

    <clear />
    <rule name="WWW Rewrite" stopProcessing="true" enabled="true">
      <match url=".*" />
        <add input="{HTTP_HOST}" pattern="^www[.](.+)" />
      <action type="Redirect" url="http://{C:1}/{R:0}" redirectType="Permanent" />

I believe this is NOT a DNS job, you may make www as CNAME for mydomain.com but still, the www will show up in the URL

If you're using Apache2 or any webserver capable of reading and executing an .htaccess file you can use the code below :

<IfModule mod_rewrite.c>

# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /

# Add or remove www.  Whichever you prefer.
# This one removes which seems to be the favourable choice these days.
# ------------------------------
RewriteCond %{HTTP_HOST} ^www.mydomain.com
RewriteRule (.*) http://mydomain.com/$1 [R=301,L]

  • I do have CNAME for www, but get the error anyway.
    – z-boss
    Jan 21 '13 at 15:06
  • @Hicham, as stated in the subject, it is about Azure, which uses IIS and not Apache.
    – Tom
    Jan 21 '13 at 19:24
  • @Tom, I googled for that before giving my answer, ant nothing forces you to use IIS on Azure. At least, that is what I read when searching Azure .htaccess Jan 21 '13 at 19:28
  • @Hicham, yes, you are right when you use, say, Linux with Apache on Azure Virtual Machines. However, Azure hosted website uses only IIS.
    – Tom
    Jan 21 '13 at 19:36
  • Fair enough, the OP will eventually tell us which environment he have ^^ Jan 21 '13 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.