homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Apache Virtual Host Redirect
Is this right for httpd.conf file

5+ Year Member

Msg#: 3549393 posted 4:22 am on Jan 16, 2008 (gmt 0)

Ok so I have been reading through Apache how to online and banging my head against the wall a little but starting to get it. Basically just trying to turn http://example.com to http://www.example.com/. I have done this successfully with .htaccess but it fails when I go to another directory which employs re-writting the url for search engine friendly urls. So I need a better solution and before I screw with my domain trying to make sure the code below will work for me. Will this change all urls on my site to prefix www? Thanks for taking a look.

<VirtualHost *>
ServerAlias example.com
Redirect permanent / http://www.example.com/



10+ Year Member

Msg#: 3549393 posted 11:34 am on Jan 16, 2008 (gmt 0)

Yep, this should do what you are looking for. An another way would be to use mod_rewrite indeed (in you normal VirtualHost), but that has some (might be very little) performance penalty, as the server's hostname needs to be checked for each request (even when it is the correct one). So placing it into a separate VirtualHost is the best you can do in my opinion.


WebmasterWorld Senior Member lammert us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

Msg#: 3549393 posted 12:21 pm on Jan 16, 2008 (gmt 0)

My first VirtualHost section in my httpd.conf is a catch-all box where all requests are going for server names that are not specified in other VirtualHost sections. This is therefore also where all the example.com requests land. I have the following settings in that VirtualHost section:

<VirtualHost my-ip-address:80>

ServerName www-redirect

KeepAlive Off

RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^\./]+\.[^\./]+$
RewriteRule ^/(.*)$ ht tp://www.%{HTTP_HOST}/$1 [R=301,L]


The KeepAlive Off closes the connection directly after the 301 redirect is sent. This makes the apache instance available for new requests. Normally Apache will keep the line open for 15 to 25 seconds, depending on the overall KeepAlive settings in your httpd.conf.

The rewrite part looks voor all requests, where the HTTP_HOST variable contains two parts, separated with a dot. If this condition is met, the rewrite rule places www. at the beginning, and glues the rest of the URL behind the domain name.

This solution works fast because there is no overhead in the VirtualHost section, no cookies sent which may be the case for the main VirtualHost etc, closes the connection to free resources and works for all domains that are pointing to my server at once. No need to program individual rewrite sections for every single site I add to my server.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved