Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up Apache redirects by putting hostnames in hosts file

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?

like image 373
Nick Russler Avatar asked Mar 29 '13 12:03

Nick Russler


2 Answers

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 :)

like image 116
Ja͢ck Avatar answered Oct 19 '22 23:10

Ja͢ck


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.

like image 41
3 revs Avatar answered Oct 19 '22 21:10

3 revs