skip to Main Content

How can I create VirtualHost to redirect all links changing only its domain, including subdomain and parameters:

exampleA.com -> exampleB.com
test.exampleA.com -> test.exampleB.com
test1234.exampleA.com/url/test.html?param=222 -> test1234.exampleB.com/url/test.html?param=222

I want to redirect all subdomains like *, and it should be permanent 301

Now I have a simple 301 redirection

<VirtualHost *:80 *:443>
        ServerName exampleA.com
        ServerAlias *.exampleA.com

        RewriteEngine On
        Redirect 301 / https://exampleB.com
</VirtualHost>

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution:

    <VirtualHost *:80 *:443>
            ServerName exampleA.com
            ServerAlias *.exampleA.com
    
            RewriteEngine On
            RewriteCond %{HTTP_HOST} (.+.)?exampleA.com$ [NC]
            RewriteRule (.*) https://%1exampleB.com$1 [R=301,L]
    </VirtualHost>
    

  2. I have never done something like this, but try the redirect option in your virtualhost file. First enable rewrite

    sudo a2enmod rewrite
    

    Then in your virtualhost file

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =exampleA.com [OR]
    RewriteCond %{SERVER_NAME} =www.exampleA.com
    RewriteRule ^ https://exampleB%{REQUEST_URI} [END,NE,R=permanent]
    

    Read more about this here: https://httpd.apache.org/docs/2.4/rewrite/remapping.html

    It takes the original domain and rewrites to another. In older apache I remember it goes something like this:

    <VirtualHost *:80>
      ServerName www.domain1.com
      Redirect / http://www.domain2.com
    </VirtualHost>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search