Something like this works for the index page:

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

<VirtualHost *:80>
ServerName http://www.example.com

So a url like this:

http://example.com gets forwarded to 

but this doesn't work:


I have also tried the following in .htaccess with the same results:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=permanent,L]

Please help me resolve this.

Full configuration:

LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
LoadModule proxy_balancer_module /usr/lib/apache2/modules/mod_proxy_balancer.so
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

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

<VirtualHost *:80>
ServerName http://www.example.com

ProxyPass / balancer://example/ stickysession=JSESSIONID|jsessionid
ProxyPassReverse / balancer://example/ stickysession=JSESSIONID|jsessionid
ProxyPreserveHost On

<Proxy balancer://example>
BalancerMember http://host1.example.com:8080/ route=tomcat1
BalancerMember http://host2.example.com:8080/ route=tomcat2

robots.txt is located on each of the tomcat servers.

marked as duplicate by Jenny D, mdpc, Diamant, ptman, MadHatter Jan 3 '16 at 10:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


I always use a negated pattern, so anything that matches the Virtual Host, but doesn't match the canonical address, will be redirected.

RewriteCond %{HTTP_HOST} !^www.example.com [nocase]
RewriteRule ^/(.*)$ http://www.example.com/$1 [redirect=permanent,nocase,last]

This has the benefit of an unlimited and unspecified number of ServerAlias directives (eg, *.example.net, *.example.org, *.example.asia etc) to all redirect correctly to www.example.com)

  • For some reason apache looks for this file in /etc/apache2/htdocs and throws a 'File does not exist error' in the error log. – Prashant Jul 5 '13 at 0:31
  • 1
    What file? I put this inside the VirtualHost block, and don't forget RewriteEngine On – fukawi2 Jul 5 '13 at 4:24
  • In my case the problem is not with the root domain redirect. The problem occurs only for sub urls. – Prashant Jul 5 '13 at 15:49
  • That's what the (.*) and the $1 deals with. It's called a back-reference and will redirect to the same path as the original request. eg, example.com/about-us will be redirected to www.example.com/about-us – fukawi2 Jul 5 '13 at 23:41

I personally have always found a simple 301 redirect to be more than adequate for this purpose:

<VirtualHost *:80>
    ServerName  oakalleyit.com
    Redirect    301 /   http://www.oakalleyit.com/

It's simple, easy to read/remember, and get's the job done right.

This is actually copied from my production web server, so I know it works.

There are docs on Apache redirects available here: http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirect

And I just tested, and typing http://oakalleyit.com/user redirects correctly to http://www.oakalleyit.com/user


This has always worked for me:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [R=301,L]

I think your missing the $ on the condition perhaps?

More info can be found here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html http://httpd.apache.org/docs/current/mod/mod_rewrite.html

  • Just noticed the link points to a non maintained version of Apache, sorry. This may also help, httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect – OneLogicalMyth Jul 4 '13 at 23:13
  • You can always edit your answer. – Michael Hampton Jul 4 '13 at 23:18
  • I felt the information was still relevant as, the information contained on the page still seems to be valid to me. It is also shows how I do my redirection at the moment as per my answer. Did think about removing it but couldn't make up my mind :-) – OneLogicalMyth Jul 4 '13 at 23:27
  • Thanks for replying but this didn't work. My underlying configuration is a bit more complicated than this ( I updated it above ). I use apache mod_proxy_balancer to forward requests to two tomcat servers. – Prashant Jul 4 '13 at 23:53

The Apache documentation page Redirecting and Remapping with mod_rewrite says:

Canonical Hostnames

Description: The goal of this rule is to force the use of a particular hostname, in preference to other hostnames which may be used to reach the same site. For example, if you wish to force the use of www.example.com instead of example.com, you might use a variant of the following recipe.


The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).

<VirtualHost *:80>
  ServerName undesired.example.com
  ServerAlias example.com notthis.example.com
  Redirect "/" "http://www.example.com/"

<VirtualHost *:80>
  ServerName www.example.com

You can alternatively accomplish this using the <If> directive:

<If "%{HTTP_HOST} != 'www.example.com'">
  Redirect "/" "http://www.example.com/"

Not the answer you're looking for? Browse other questions tagged or ask your own question.