Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

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]

</IfModule>
share|improve this answer
    
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 –  Hicham LEMGHARI 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 ^^ –  Hicham LEMGHARI Jan 21 '13 at 21:42
up vote 0 down vote accepted

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:

<system.webServer>
<rewrite>
  <rules>
    <clear />
    <rule name="WWW Rewrite" stopProcessing="true" enabled="true">
      <match url=".*" />
      <conditions>
        <add input="{HTTP_HOST}" pattern="^www[.](.+)" />
      </conditions>
      <action type="Redirect" url="http://{C:1}/{R:0}" redirectType="Permanent" />
    </rule>
  </rules>
</rewrite>
</system.webServer>
share|improve this answer
    
Could you expand on how this is done? "explicitly set up in Azure as custom domains" –  IrishChieftain Feb 23 '13 at 13:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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