0

I was told to ask on Server Fault when I asked on Super User, and that makes a lot of sense to me, so here it is.

I like the look of no www but am somewhat worried about drawbacks, such as shared cookies across the root and all subdomains (not that I actually know anything much about cookies at the moment, but I do want to futureproof).

My guess is this might possibly be possible through some sort of alias type thing. But I could be wrong, so here I ask.

So, to clarify, is it possible to have http://foo.com/ in the address bar while http://www.foo.com/ is actually being accessed?

Edit: I'm planning to host with NearlyFreeSpeech.NET. The server is Apache and I would guess the OS is some flavor of Linux.

Edit 2: So do you actually understand what I mean? What I'm looking for is this: when one browses to the site, http://foo.com/ is what they see in the address bar but http://www.foo.com/ is what is being accessed. I have to say Xorlev's answer looks the closest but will it work out, considering potential cookie issues and that I'm going to be using shared IP hosting.

Nathaniel
  • 135

8 Answers8

7

Yes. Depending on the web server configuration, you either need to not do anything (if using IP-based virtual hosts or not using virtual hosts at all) or configure a name-based virtual host with a ServerName and ServerAlias directive.

In Apache, that would look like:

NameVirtualHost *:80
<VirtualHost *:80>
ServerName foo.com
ServerAlias www.foo.com
# Virtual host document root, logging, other configuration here
</VirtualHost>

You will also need DNS for both foo.com. and www.foo.com. to resolve to the same address.

6

I would comment, if I were allowed to.

Charles' answer is correct. One thing you might want to do a little differently. I normaly set the foo.com DNS entry as an A record to the IP and the www.foo.com entry as a CNAME record to foo.com. So In case your IP changes, you don't need to change both. But that's a small detail.

markus
  • 612
  • 2
  • 9
  • 18
3

I don't think any of these answer it. Your main problem is going to be with cookies: if you set them at example.com, the browser will send them in for *.example.com, including domains that serve static content such as images.example.com or static.example.com. Your problem then is that an interim cache, such as a caching proxy or even a browser, may refuse to cache anything that was requested with cookies. This sends the load back to your primary server.

Many people recommend having a www subdomain so that you can serve your cookies from there and not have them sent back for your static subdomain. Unfortunately, if you do this, you cannot get a no-www URL. My recommendation is to use an entirely different top level domain for static content:

Main: example.com
Static: examplestatic.com

Look at twitter.com as an example. They don't have a www and they serve images and CSS from twimg.com.

Jace
  • 141
1

You could rewrite the url if it includes www.

Something along the lines of:

RewriteEngine On
RewriteCond %{HTTP_HOST}   ^www\.NearlyFreeSpeech\.NET [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://NearlyFreeSpeech.NET/$1 [L,R=301]

Not entirely sure that'll work out of the box but should be close.

Xorlev
  • 1,845
1

Independent of which domain you choose - keep in mind to be consequent with it. Indexing from search engines are affected by your choice.

i.E. Google: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=44231

jomey
  • 312
1

At the main page of example.com, you can show the site www.example.com inside an iframe, so while the user goes to and sees example.com, the cookie will come from www.example.com.

hayalci
  • 3,671
  • 3
  • 28
  • 38
0

I don't know about the cookies, but as far as accessing your web site via http://example.com and http://www.example.com is concerned, you need to add a domain A record to your DNS zone that points to the ip address of your web server. You may or may not have to add hostheaders to your web site for this to work, I don't remember and don't have a server handy to check.

0

Yes, it's possible, and I recommend it.

You probably want to add a permanent redirect so that "example.com/*" redirects to "www.example.com/". You don't say what web server you use, but it should be easy enough in any one of them.

Teddy
  • 5,294
  • Actually, that's now quite what I wanted to do. I want to have http://foo.com/ when http://www.foo.com/ is being accessed. – Nathaniel Oct 05 '09 at 19:12