We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 727
    • 502 Posts
    I would like to have two virtual hosts with different subdomains point to the exact same site. I want to do this so I can switch between apache commands without having to keep restarting apache.

    OK, so I tried creating two vhost declarations, but I found that [(site_url)] is not updated to the new one I am adding. This means that Wayfinder does not use the second subdomain. Here is an example:

    <VirtualHost *>
        DocumentRoot /data/websites/test.com/public_html
        ServerName testcom.foo.dnsalias.net
    </VirtualHost>
    
    <VirtualHost *>
        DocumentRoot /data/websites/test.com/public_html
        ServerName secondtestcom.foo.dnsalias.net
    </VirtualHost>


    Everything works great when using http://testcom.foo.dnsalias.net, but when I try to use secondtestcom.foo.dnsalias.net, the first page loads ok, but the <base href... contains testcom.foo.dnslias.net. This makes all links go back to the first subdomain.

    I hope that makes sense. Note that I don’t want two different sites. I want the same site but two subdomains. I am using apache2.

    I am using 0.9.6.1 and friendly URLs.

    Any ideas? Thanks, Andy
      • 22303 MODX Staff
      • 10,725 Posts
      First, if you do this, make sure don’t cache any pages...as the first domain to access it would be cached and cause URL’s to switch in what seems like unpredictable ways.

      Second, I don’t recommend this, as this is considered duplicate content and will count against you in Google I believe.

      Finally, you should not define multiple virtual hosts as * (this is to define any that don’t match ServerName or ServerAlias in any of the additional VirtualHost blocks, i.e. a catch-all of sorts); make each virtual host specific to the ServerName it’s serving. In most configurations, the VirtualHost and/or ServerName directly affects what PHP gets as the HTTP_HOST $_SERVER variable (see Apache documentation for your specific version for more detailed information). MODx uses this HTTP_HOST to build the URL’s for the site based on what the server reports, so you’ll need to configure that at the server level.
        • 727
        • 502 Posts
        Thanks! I’ll give that a try.

        Note this is only for my dev server. I want to use things like mod_cband for testing.

        Andy
          • 727
          • 502 Posts
          Changing the * to anything else generates errors in Apache, so instead I used the ServerAlias directive. For example:

          <VirtualHost *>
              DocumentRoot /data/websites/test.com/public_html
              ServerName testcom.foo.dnsalias.net
              ServerAlias testcom.foo.dnsalias.net
          </VirtualHost>
          
          <VirtualHost *>
              DocumentRoot /data/websites/test.com/public_html
              ServerName secondtestcom.foo.dnsalias.net
              ServerAlias secondtestcom.foo.dnsalias.net
          </VirtualHost>


          I can access the manager using either subdomain. For secondtestcom I’ve limited the speed of Apache to 56kbps, and it is noticeable. However for both subdomains, even with the MODx and browser caches cleared, the base href is still testcom.foo.dnsalias.net.

          If I log into the manager using each subdomain in turn and look at phpinfo I see:

          _SERVER["HTTP_HOST"] = secondtestcom.esa.dnsalias.net:7780


          and

          _SERVER["HTTP_HOST"] = testcom.esa.dnsalias.net:7780


          Any ideas? Andy
            • 22303 MODX Staff
            • 10,725 Posts
            @Andy: Configuring you server with Apache is a very complex subject, and I’m going to have to defer you to the Apache documentation at this point.

            If anyone else has pointers, feel free to jump in...
              • 727
              • 502 Posts
              OK, how about this? Where exactly does the base href value come from? I am using:

              <base href="[(site_url)]" />


              As you can see from my snippets before, HTTP_HOST is being updated correctly in the manager, so it can’t be that. If I know which PHP variable to focus on it would be a huge help in working out the Apache config.

              Thanks! Andy
                • 22303 MODX Staff
                • 10,725 Posts
                It is a global variable (and constant, i.e. MODX_SITE_URL) that is determined in the config file, using $_SERVER[’HTTP_HOST’] -- but HTTP_HOST can be affected by many things in the Apache configuration (depending on your Apache version), most notably, it will typically reflect the host it was accessed with (based on the headers sent by the client). See UseCanonicalName Directive for additional information on this and using virtual hosts.
                  • 31337
                  • 258 Posts
                  Jason is exactly right. Using "<VirtualHost *>" for more than one domain is improper and will eventually break your Apache config in unexpected ways.

                  I am not 100% sure what you’re trying to accomplish based on your examples, but I’d encourage you to read http://httpd.apache.org/docs/1.3/vhosts/ and then ask for help if you have specific questions.