Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 just bought the domain, I added a DNS rule for and, then I tried to get apache2 to redirect the latter to the former. I enabled mod_rewrite, appended /etc/apache2/httpd.conf, which follows, and restarted the apache2 service.

ServerName localhost
AccessFileName .htaccess

# Redirect www to non-www
RewriteEngine On
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[url][/url] [NC]
RewriteRule ^(.*)$ [] [L,R=301]

I must have made some mistake, since the www domain does not redirect. I don't pretend that I understand mod_rewrite, so I'm wondering if someone here knows what the issue is.

share|improve this question
Are those [url] tags in the Apache config file? You might want to remove them. – Ladadadada Jan 31 '12 at 15:00
up vote 3 down vote accepted

I would suggest you to do this:

<VirtualHost *:80>
    #add aliases if any
     RewriteEngine on
     RewriteCond %{HTTP_HOST} ^www\.vas\.im$ [NC]
     RewriteRule ^{REQUEST_URI}%{QUERY_STRING} [L,QSA,R=301]

<VirtualHost *:80>
    DocumentRoot /path/to/documentroot
    ErrorLog logs/your_log_file_name
    CustomLog logs/your_log_file_name common

Alternatively, you can change the first virtual host block to: For this method you have to use mod_alias's Redirect. So, make sure you have loaded the mod_alias module in your .conf file.

<VirtualHost *:80>
    #add aliases if any
     RedirectMatch 301 /(.*)$1
share|improve this answer
+1. Do not use mod_rewrite unless it's unavoidable - and certainly not when it's plainly unnecessary. – adaptr Jan 31 '12 at 16:15
It works! Thank you. For some reason the whole internet seems to love the mod_rewrite way of doing this. Oh well. – Vasiliy Sharapov Jan 31 '12 at 18:13
@VasiliySharapov: I've noticed the internet at large, almost always, promotes bad practice. Yes most common copy+pasted config lines work as expected, but usually it's never the proper method. BTW, that RedirectMatch .. line does not need regex. It should be written as. Redirect 301 / – TechZilla Aug 2 '12 at 18:08

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.