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.

link|improve this question
1  
Are those [url] tags in the Apache config file? You might want to remove them. – Ladadadada Jan 31 at 15:00
feedback

1 Answer

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>
link|improve this answer
+1. Do not use mod_rewrite unless it's unavoidable - and certainly not when it's plainly unnecessary. – adaptr Jan 31 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 at 18:13
feedback

Your Answer

 
or
required, but never shown

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