Best name for your local website domain for dealing with cookies? (ex: localhost)

I’ve been encountering problem after problem switching from one browser to another due to the different ways browsers interpret cookies on “malformed” domains like http://localhost/. Example - IE will not accept cookies from localhost (http://stackoverflow.com/questions/1134290/cookies-on-localhost-with-explicit-domain); and Chromium might not either (haven’t tested it myself: http://code.google.com/p/chromium/issues/detail?id=56211). I can get IE to keep cookies if I change my cookie’s domain to 127.0.0.1 AND go to my site in my browser at 127.0.0.1/mysite (instead of localhost/mysite).

I also work with subdomains, which complicates cookies further, mostly due to IE dropping cookies for domains with _ in them or starting with a number.

I found all of these technical curiosities out the hard way, and want to avoid future surprises.

I’d like to have a setup that allows something, as similar as possible, to localhost/projects/project1 or dev.localhost/ . But I am trying to avoid switching to a setup that won’t work for whatever unforeseen reason.

In the interest of being able to work with a site locally, what are the parameters you should use to name your site’s domain, sub domain, what should you use as the cookie domain for cookies (for cookie compatibility across browsers)?

If no one can comment on what parameters to use, what do people use in practice, and have you encountered any problems?

I simply set up subdomains at localhost for my sites, each site has its own subdomain, eg. shop.localhost, carservice.localhost, etc. Sometimes I set up subdomains on subdomains but I rarely need that.

I am surprised by these IE cookie problems on localhost, because I’ve never experienced any. I haven’t done any extensive test cases on IE but quite often I have a local site that uses cookies for authorization, sessions or other things and I can’t remember having any problems logging in or using my sites on localhost in IE. Also, when a local web site uses javascript to create cookies it also works fine in IE. I still have IE8 installed for testing purposes but I also test in higher IE versions via virtual machines and they also work fine with cookies - virtual machines use the same *.locahost subdomains for access as my host system.

Maybe there are some specific IE settings that restrict what it can do on localhost? I haven’t really played with them much and for me the default settings always work well for localhost. However, I’ve had problems with web pages accessed on local filesystem via the file:// protocol but I don’t really need that so much, anyway.

The other stackoverflow post says that the subdomain cannot begin with a number (1,2,3…), or contain an underscore for the issue to arise… Do any of your subdomains do this? And if they do, do you have problems with cookies in IE?

I’ve just tried 123.localhost for one of my online shops and in IE8 all services that use cookies work fine: shopping cart, authorization to user and admin panel, cookie info on the site (dismissed by js setting the cookie). Maybe there are some edge cases where cookies wouldn’t work but I have yet to come across them.

Edit: For 12_3.localhost indeed cookies in IE8 do not work. But as far as I know underscores are not allowed in real domains and subdomains, are they?

That’s right, they aren’t, but they still work in other browsers. You might want them to work because a subdomain is set to match some other term, like one provided by a user (maybe their name, email). I don’t mind appealing to IE’s idiosyncrasies though (well, I got used to it, at least), I just don’t want to run into some issue like this I don’t know about.

Which version of IE did you try that worked with 123.localhost?

My reasoning is that it is best (most convenient) to mirror a production site to its local counterpart as closely as possible so if I can’t use a certain subdomain in the real world (because of an underscore) then I’m not trying to get it to work on localhost, either. I know there can be uses for underscores but I just accept I have to avoid them and deal with it in other ways. IE team probably figured out they don’t want to support anything more than is absolutely required.

IE8 on Win7 and IE11 on Win8.1 in a virtual machine.