0

All of the names currently point to the same web server folder which is correct. That is, the domain.net, domain.org, etc all work without redirecting to the main .com

How do I get visits to the .org and .net to redirect the browser to domain.com?

This should be basic, but I am can't figure out the best approach.

2 Answers 2

3

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


2
  • This is a good solution if your redirects are tricky or conditional, but if you just need to change hostnames whilst preserving paths the Redirect solution is simpler IMO (and more readable).
    – nickgrim
    Jan 19, 2012 at 11:44
  • @nickgrim I agree its simpler. I am trying to take care of most cases here. Jan 19, 2012 at 12:56
2

@Pasta,

What I do in this particular situation is to create a separate virtualhost block

<VirtualHost *>
   ServerName domain.net
   ServerAlias domain.org

   Redirect permanent http://domain.com
</VirtualHost>

Hope that helps.

Ismael Casimpan

5
  • Thanks, is there a difference between ServerName, ServerAlias?
    – Pasta
    Jan 19, 2012 at 11:21
  • With that solution, a request to say http://domain.org/articles/view/123/comments will just be redirected to http://domain.com/ without the rest of the URI. That's not too bright if people have bookmarked deeplinks to the .org or .net hosts for example Jan 19, 2012 at 11:30
  • @Pasta You can only have one ServerName, and it's intended to be canonical. You can have as many ServerAliases as you like. Docs are here.
    – nickgrim
    Jan 19, 2012 at 11:38
  • 2
    @JudasIscariot1651 That's completely not true; the documentation for Redirect states "Additional path information beyond the matched URL-Path will be appended to the target URL".
    – nickgrim
    Jan 19, 2012 at 11:41
  • @nickgrim I apologize for my ignorance, I seldom use mod_alias, always mod_rewrite Jan 19, 2012 at 13:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .