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

I want redirect all traffic to example.com/* to be redirected to www.example.com/*

I found How can I redirect any ServerAlias to the respective ServerName? which handles the topic in question and did what was recommended in the answer with four upvotes:

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

However, this redirect traffic going to http://example.com/subdirectory to http://www.example.com and not to http://www.example.com/subdirectory as desired.

Any medication on this issue?

share|improve this question
    
Your rewrite is ok. Works as intended (tested :). It must be something else with your configuration. –  RJS Oct 15 '12 at 16:55
    
Nope, it's not okay :) –  adaptr Oct 15 '12 at 17:17

1 Answer 1

up vote 2 down vote accepted

The correct way to do this is to create a new vhost and redirect in that:

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

<VirtualHost *:80>
  ServerName www.example.com
  DocumentRoot /var/www/something
</VirtualHost>
share|improve this answer
    
Thank you so much! This works. –  budwiser Oct 15 '12 at 19:14

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.