Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to set a redirection in my VirtualHost configuration for my website. So far I am able to redirect non www trafficto the www address like this :

<VirtualHost: *:80>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^guidedescommercesdebeauce\.com$ [NC]
    RewriteRule ^(.*)$$1 [R=301,L]

But what I also want is to redirect the old domain to this new one. I have tried adding :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^guidedescommercesdebeauce\.com$ [NC]
RewriteCond %{HTTP_HOST} ^gcbeauce\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

But nothing happens.

Am I missing something here?

share|improve this question
While your machine might be running Ubuntu, an Apache question like this one might get a better result on – Windigo Feb 1 '11 at 16:26

You need to negate the match. Try this:

RewriteCond ${HTTP_HOST} !^www\.guidedescommercesdebeauce\.com$ [NC]
RewriteRule ^(.*)$$1 [R=301,QSA,L]

Notice the exclamation mark in the RewriteCond. Note also that this will force a single canonical domain name. I'm assuming this is what you want. If not, then you'll have to enumerate the domain names to be redirected. In that case, be sure to add [OR] to the end of the RewriteCond.

I've also added [QSA] to your flags. That way, any query string that might exist will be preserved.

Finally, you are strongly urged to read the mod_rewrite documentation carefully, as it has answers to this and many other questions.

share|improve this answer

Missing a slash before $1?

RewriteEngine On
RewriteRule ^(.*)$$1 [R=301,L]
share|improve this answer
Adding the extra slash will make the url looks like : in this case. – Sebastien Lachance Feb 1 '11 at 16:09

If you are using Apache 2.4 ,without the need to enable the rewrite apache module you can use something like this:

# to
<If "%{HTTP_HOST} = ''">
  Redirect 301 "/" ""
share|improve this answer

Your Answer


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.