6

i am using apache and have a domain e.g.: www.example.com. I redirected example.com to www.example.com by using this:

https://stackoverflow.com/a/1100363/441907

I also have a virtual host entry for www.example.com.

I had then performance issues which were resolved by putting www.example.com into my hosts file for 127.0.0.1.

My Question is:

Would i gain performance improvements by also putting example.com into the hosts file or does this make no sense since it simply gets resolved to www.example.com which is already in the hosts file?

Community
  • 1
  • 1
Nick Russler
  • 4,608
  • 6
  • 51
  • 88
  • The performance losses from the DNS lookup are going to be less than the performance losses from the apache redirect call. An apache redirect incurs a full travel time penalty since the server has to tell the browelser that the page has moved. The browser then sends out another request for the new page. – kipple Apr 06 '13 at 18:17
  • I am sorry but i don't get your answer :) Should i put example.com into my hosts file? (Would it give me a speed improvement?) – Nick Russler Apr 06 '13 at 18:42
  • Why would you ask (twice) instead of just trying it? I don't understand what your performance issue was, or whether you put your workaround on the client or the server, but if you're redirecting from A to B, your client will be resolving both domains and the server will be resolving neither under a normal configuration. – covener Apr 07 '13 at 00:00

2 Answers2

3

If you want to avoid any DNS lookups, you should put both entries in your hosts file:

127.0.0.1 www.example.com example.com

Adding example.com speeds up the first request made to http://example.com, whereas having just www.example.com speeds up the subsequent page loads after it's been redirected.

In other words, taking out example.com only matters if a) you're accessing the site via its non-canonical name and b) you're actually worried about the one-time delay it incurs.

Having said that, it should be mentioned that this is only useful during development though :)

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • You said it was only useful during development but i had serious delay issues when not having www.example.com in the hosts file (more than 2s delay). Or did you meant that putting example.com into the hosts file would increase the speed only during development for some reason? – Nick Russler Apr 10 '13 at 16:47
  • @Nick adding custom entries in your hosts file is only practical when you're doing active development on a site you have running locally. Other people visiting your site would typically not do that :) – Ja͢ck Apr 10 '13 at 22:53
  • No, i didn't mean that i put those in my hosts file, i putted them into the host file of the server which resolved heavy issues (i found this tip here on stackoverflow, i will look the thread up) – Nick Russler Apr 11 '13 at 11:40
  • @nick well, most of my answer still applies, but the server shouldn't do many dns lookups as part of its per-request operation in the first place. – Ja͢ck Apr 11 '13 at 12:10
  • Does the server make any dns lookups for the redirection (example.com)? Or does it only redirect to www.example.com internally? (This is how i should have phrased this question in the first place...) – Nick Russler Apr 12 '13 at 17:03
  • @Nick Ah. No, as far as the web server is concerned these are all string operations. – Ja͢ck Apr 12 '13 at 23:22
1

I think, putting example.com into your hosts file does make a little bit of no sense, since the ServerName example.com is permanently redirected to www.example.com/ according to the configuration that you've linked. And you can also edit your question in detail, so others could get your performance issue, and even I could improved this answer if I can guess. Actually, YOU can test it by yourself, if the performance increased with example.com into your hosts file, I think by comparing their speed on such this kind of site http://network-tools.com/default.asp?prog=dnsrec&host= but I'm not sure about that. However, I'm sure that there's a software application for testing performance.

You could still temporary removed the redirection of example.com and check if it have an issue like www.example.com, if ever it have then try to put it into the hosts file. And if it be fixed by doing that, for me I would let that domain into hosts file even if I'm going redirect it to its top-level domain. If example.com ever have the performance issue like www.example.com before, maybe that performance will also affect the speed of URL redirecting.