Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

3 Answers

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)

share|improve this answer
    
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
add comment

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

share|improve this answer
    
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
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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