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.

I just bought the domain vas.im, I added a DNS rule for vas.im and www.vas.im, 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]www.vas.im[/url] [NC]
RewriteRule ^(.*)$ [vas.im...] [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
1  
Are those [url] tags in the Apache config file? You might want to remove them. –  Ladadadada Jan 31 '12 at 15:00

1 Answer 1

up vote 3 down vote accepted

I would suggest you to do this:

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

<VirtualHost *:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot /path/to/documentroot
    ServerName vas.im
    ErrorLog logs/your_log_file_name
    CustomLog logs/your_log_file_name common
</VirtualHost>

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>
     ServerName www.vas.im
    #ServerAlias vas.net
    #add aliases if any
     RedirectMatch 301 /(.*) http://vas.im/$1
</VirtualHost>
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 / http://vas.im/ –  TechZilla Aug 2 '12 at 18:08

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.