3

I don't want "www" in my domain. How do I set dns record so that when someone types www.mydomain.com it will just show up as mydomain.com?

I just want www to foward to root domain? Tried searching but google only tells me how to make my domain "www" which I do not want.

Is DNS even what I should be looking at?

Tool Tips is telling me, "The question you're asking appears subjective and is likely to be closed"...

Look, I don't know how to ask the question any different. I thought this could be done on a DNS level without messing with htaccess.

Any help would be appreciated.

2
  • DNS is oblivious to redirects it simply translates names into IP addresses; therefore if you actually want to redirect www. to non-www you would have to use htaccess to redirect it.
    – Analog
    Commented Apr 17, 2016 at 23:58
  • 1
    The DNS (eg. CNAME record) will need to be configured to accept requests to the www subdomain, but then redirecting to the bare domain is not a DNS issue (as stated). If using Apache, then this can be done in the server config (or .htaccess). Related: Redirect www to non-www using Apache htaccess
    – MrWhite
    Commented Apr 18, 2016 at 9:18

2 Answers 2

2

While people expect not having to type the www., most web servers will add it once you connect to them because that is its canonical name. Try it with google; typing google.com into your browser takes you to www.google.com.

But try this: configure your web server to accept www.example.com and example.com as acceptable names, with example.com as the canonical name, i.e. for Apache2:

ServerName example.com
ServerAlias www.example.com

Then, for a hosted DNS setup, configure your DNS for the domain so that the @ record is an A record with your IP address and www record is a CNAME for example.com.

If you're using bind text config files, try something like this to use the same host at IP 1.2.3.4 as the www server and domain origin:

$ORIGIN .
$TTL 43200      ; 12 hours
example.com          IN SOA  ns.example.com. root.example.com. (
                                201511496 43200 7200 604800 3600 )
$TTL 604800     ; 1 week
                        NS      ns1.example.com.
                        A       1.2.3.4
$ORIGIN example.com.
# web server is hosted on same host as domain origin
www                     CNAME   example.com.

To then FORCEFULLY redirect www.example.com to example.com regardless of what the user expects, use a rewrite rule in your web server. For example, in Apache2:

RewriteCond %{HTTP_HOST}   !^example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://example.com/$1 [L,R]

For further Apache2 rewrite examples and for non-port 80 hosts, see https://httpd.apache.org/docs/2.4/rewrite/remapping.html

3
  • This answer does not address the redirect part of the OPs question. Can you add a simple .htaccess example? Good detail otherwise. I also like to recommend that the ServerName (www.example.com) and ServerAlias (example.com) be reversed. Some control panels set these backwards with the assumption that www is preferred. It will work the way it is set, however, it is technically better the other way around. Cheers!!
    – closetnoc
    Commented Apr 18, 2016 at 22:13
  • @closetnoc changed as indicated. Also added info on forcefully rewriting the domain name with a rewrite rule; this could go in the server config or in an .htaccess file. For sites that DO prefer the www prefix, why do you say ServerName config without it is technically better? Should ServerName not be the canonical name?
    – simpleuser
    Commented Apr 18, 2016 at 22:38
  • I do not remember the specific reason, it just may be backwards logic in my book. Because technically, you create the domain name, then add any sub-domain as an alias. It is just a few install scenarios that I have seen that set it backwards. cPanel is one of them. Like I said, it will work just fine. I am just being an old nit-picky git!! I would up-vote again if I could! I have tried. It just treats me like an old git. Cheers!!
    – closetnoc
    Commented Apr 19, 2016 at 0:51
1

There is no DNS record under the RFC which will forward your connection to the non-www version of your site. The traditional way to do this is to set your web server IP as an A record for your zone apex and then set www as a CNAME record referencing the zone apex of your domain but you specifically say that you do not wan't to do this. In this case the only way to do what you are wanting is through code, more specifically, setting up a site under www with only a .htaccess file on it and set it up to do a 301 redirect to your zone apex site but to be honest this is not the best practice to do and understand that most users on the internet don't understand DNS or routing and would expect to see the www there if they are trying to access the site.

2
  • "and would expect to see the www there" — It is 2016. People are used to not having www. at the front of a URL these days.
    – Quentin
    Commented Apr 18, 2016 at 8:19
  • @Quentin I strongly disagree. Most sites still add www for the front page, and only discard it for specific subdomains within the main site, such as store.example.com or tutorial.example.com etc. I say this after a quick examination of the 84 tabs I currently have open, all to different sites.
    – simpleuser
    Commented Apr 18, 2016 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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