5

Something like this works for the index page:

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

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

So a url like this:

http://example.com gets forwarded to 
http://www.example.com

but this doesn't work:

http://example.com/robots.txt

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>

<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
</Proxy>
</VirtualHost>

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

4 Answers4

8

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)

fukawi2
  • 5,494
7

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/
</VirtualHost>

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

Soviero
  • 4,426
2

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

0

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.

Solution:

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>

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

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

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