I would suggest you to use RewriteRule
from mod_rewrite
than use Redirect
from mod_alias
. As mod rewrite is more versatile.
<VirtualHost *:80>
ServerName domain.org
ServerAlias www.domain.org domain.net www. domain.net
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.(net|org)$ [NC]
RewriteRule ^ http://domain.com%{REQUEST_URI} [L,R=301]
</VirtualHost>
<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /path/to/documentroot
ServerName domain.com
ServerAlias www.domain.com
ErrorLog logs/dummy-host.example.com-error_log
CustomLog logs/dummy-host.example.com-access_log common
</VirtualHost>
Make sure you have NameVirtualHost *:80
uncommented.
Explanation:
RewriteEngine on
Sets the RewriteEngine
on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.(net|org)$ [NC]
Matches %{HTTP_HOST}
: i.e: The base URL of the host. For more on RewriteCond
& http headers
visit: RewriteCond.
[NC]
is a flag: 'nocase|NC' (no case)
This makes the test case-insensitive - differences between 'A-Z' and 'a-z' are ignored, both in the expanded TestString and the CondPattern. This flag is effective only for comparisons between TestString and CondPattern. It has no effect on filesystem and subrequest checks.
Also you have to redirect URL's that start with WWW as well.
that is why ^(www\.)
in the pattern.
RewriteRule ^ http://domain.com%{REQUEST_URI} [L,R=301]
Rewrites the URL to domain.com
if a match in RewriteCond
Occurs. Adds any request URI if present. i.e. The URI for this request (relative to DOCUMENT_ROOT).
if URl was: domain.com/web/hello.php
then REQUEST_URI=/web/hello.php