domain redirection with virtualhost

25,462

Solution 1

DocumentRoot is not required. You can set up vhosts like this without any problems:

 <VirtualHost xx.xx.xx.xx:90>
  ServerName domain.tld
  ServerAlias www.domain.tld
  RedirectPermanent / http://www.domain2.com/
 </VirtualHost>

This will redirect all requests to the main site but also subfolders. E.g. www.domain.tld/foo/bar will be redirected to www.domain2.com/foo/bar

ServerAlias is only required if you want several hostnames.. Such as both www and without www.

Solution 2

You have mixed up .com and .de in your example configuration. Could this be the problem?

Question 1:

RewriteCond %{HTTP_HOST} ^.*\.test1\.de [NC]
RewriteRule ^(.*)$ http://www.test.de/$1 [L,R=301]

Question 2:
I'm not a 100% sure if a DocumentRoot is required or not, but I think no.

Question 3: Yes, you need the *.test1.com Alias.
It might work without it if this VirtualHost is your first VirtualHost.
It would then get all requests that did not match any other VirtualHosts as it is the default.
However it's cleaner to configure it.

Solution 3

Try something like this in your virtualhost:

<Virtualhost XXX.XXX.XXX.XXX:80>    
     ServerName www.test1.com
     ServerAlias *.test1.com
     Redirect 301 / http://www.test.com
</VirtualHost>

Very basic and should transfer anything regardless of what url the user is trying to access from test1.com to test.com.

Share:
25,462

Related videos on Youtube

mahatmanich
Author by

mahatmanich

Updated on September 18, 2022

Comments

  • mahatmanich
    mahatmanich almost 2 years

    I have setup a domain, I and I would like to point that domain onto a different webserver.

    lets say:

    domain 1: www.test.com on server 1

    domain 2: www.test1.com on server 2

    I would like to forward www.test1.com to www.test.com, and I have tried to do that with setting up a virtual host on server 2 and mod_rewrite.

    The virtual host looks like this:

       <virtualhost XXX.XXX.XXX.XXX:80>
                ServerName   www.test1.com
                ServerAlias  test1.com *.test1.com    
                RewriteEngine On
                RewriteCond %{HTTP_HOST} (.*)\test\.com
                RewriteRule ^(.*) http://%1test.com/$1 [R=301,L]
        </VirtualHost>
    

    Everything works fine except that http://test1.com does not redirect.

    Question 1: How would I go about redirecting all requests (subdomains everything) that come into http://test1.com to http://www.test.com using mod_rewrite.

    Question 2: do I need to specify a directory in this case for the virtual host?

    Question 3: do I need the ServerAlias or is it counter productive here?

    UPDATE:

    Ended up with doing a 301 with following setup

    <virtualhost <ip>:80>
            ServerName   test1.com
            ServerAlias  www.test1.com
            Redirect 301 / http://www.test.com
    </VirtualHost>
    
  • mahatmanich
    mahatmanich about 13 years
    should be all com sorry. that is not the issue
  • faker
    faker about 13 years
    it's missing ServerAlias *.test1.com
  • mahatmanich
    mahatmanich about 13 years
    yes that is what I want, but just with a 301 thanks :-)
  • Frands Hansen
    Frands Hansen about 13 years
    FYI, the "RedirectPermanent" does a 301 redirect. To be sure that all requests to subfolders are redirected as well, append a slash to the URL in your redirect line.
  • mahatmanich
    mahatmanich about 13 years
    how about all subdomains?
  • zed
    zed over 7 years
    Do not forget the backslash at the end of http://www.domain2.com/.
  • Nuno Sarmento
    Nuno Sarmento almost 4 years
    ServerAlias is safer :)