I want to use the keep-alive
feature in Apache. How can I do this with my host (.htaccess file), and what are the best values for the parameters like KeepAliveTimeout
?
7 Answers
If Keep-alive is turned on in the Apache configuration, all you need is just set an HTTP header Connection: keep-alive. E.g. add following lines to your .htaccess file:
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>

- 1,359
- 1
- 12
- 8
-
5You sir, deserve more praise for this. – PaulSkinner Oct 02 '12 at 15:17
-
For more ifmodule keep alive settings see http://httpd.apache.org/docs/2.2/mod/core.html#keepalive – Blowsie Jan 23 '13 at 09:25
-
4I would insist on "IfModule" instead of "ifModule" anyway. – Hermes May 16 '13 at 16:41
-
Can anyone guess why this doesn't work for me in Apache 2.4? KeepAlive is set to off in in apache2.conf. I can enable it via vhost conf file but not in htaccess. – But those new buttons though.. Jan 05 '17 at 15:51
You can't control keepalive behaviour in an .htaccess
. Keepalives are a host-level feature, not one where different directories can behave differently depending on the per-directory htaccess info.
If you are on the kind of basic shared hosting that only gives you .htaccess
to configure your sites, you can't change the keepalive settings. Presumably the hosting company will have set them appropriately, or just left them on the default settings, which are usually fine.

- 528,062
- 107
- 651
- 834
-
Thanks bobince ,what should be the timeout time , as my site doesn't have a great amount of traffic thanks – webkul Feb 04 '10 at 15:19
-
If your hosts haven't changed it, the default in Apache 2.0 is 15 seconds, and in 2.2 it's 5 seconds. Either setting is perfectly reasonable. – bobince Feb 04 '10 at 15:23
-
2Sorry but in Apache you can use the .htaccess adding this code
Header set Connection keep-alive ... and it works! About the timing, the server default, often, is good. – Simbus82 Oct 03 '14 at 09:53 -
better answers from anil kumar & pronskiy below --- `
Header set Connection keep-alive ` in `.htaccess` works for me as well. Here is another helpful article: http://www.feedthebot.com/pagespeed/keep-alive.html – retrovertigo Dec 29 '14 at 22:53 -
I have root access to the server, but i dont want to enable keep alive for all my sites, so its not possible to enable to just one? – Arnold Roa Oct 22 '16 at 13:35
Yes Keep-alive behavior can be controlled in .htaccess file.
First check the server setting by printing $_SERVER
and if
[HTTP_CONNECTION] => keep-alive
is there then you just have to include the setting in your .htaccess file. Add the following line at the end of .htaccess file in your project's root directory.
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>

- 8,100
- 16
- 64
- 86

- 151
- 1
- 5
If you have SSH access to your server you should edit the Apache config file. Use these settings as a starter:
- KeepAlive: on
- KeepAliveTimeout: 3 seconds
- MaxKeepAliveRequests: 60
This should work for most basic server setups with average traffic. You can always tweak the settings to suit your own needs. See here for more detailed info about this: http://www.giftofspeed.com/enable-keep-alive/
If you don't have access to your server you should contact your host. Changing the keepalive settings on your own by editing the .htaccess file will probably don't work.

- 139
- 2
It very much depends on your site and the amount of traffic it receives. If a user comes to your site, then clicks through to another page within the KeepAliveTimeout setting (default is 15), a new TCP does not have to be created. This can really help with overhead.
On the other hand, any Apache processes that are currently tied up w/ existing visitors will not be able to talk to the new ones. So you may have to increase the total number of Apache processes that are available.
In short... it requires tweaking.

- 3,009
- 2
- 23
- 26
-
thanks for the answering but the keep-alive is not working in my site how can i enable and my site doesn't have a lot amount of traffic thanks – webkul Feb 04 '10 at 15:01
you can't control keep-alive behavior in .htaccess
Paste the following code in your .htaccess file:
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
Then use this website: https://varvy.com/pagespeed/ to check if it's enabled.