Multiple Domains and Subdomains on one IP Apache2

Apache2subdomainvirtualhost

I have two virtual hosts on one IP, www.example.com and www.example2.com. The default, example.com, has a subdomain, docs.example.com that works fine, and it also has an SSL certificate, and it forces https on example.com.

When I try to set up a subdomain on the second domain, i.e. blog.example2.com (wordpress), it redirects back to the first domain, https://www.example.com. www.example2.com works fine though.

Is it possible to have subdomains for two sites on one IP? Or can you only have subdomains for the default? Is there any way to get subdomain on example2 to work?

Config files:

000-default.conf

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    ...
    DocumentRoot /var/www/example.com/public_html
</VirtualHost>

example.com.conf

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias example.com
    ...
    DocumentRoot /var/www/example.com/public_html
</VirtualHost>
<IfModule mod_ssl.c>
    ... certificates set up etc.
</IfModule>

docs.example.com.conf

<VirtualHost *:80>
    ServerName docs.example.com
    ...
    DocumentRoot /var/www/docs.example.com/public_html
</VirtualHost>

example2.com.conf

<VirtualHost *:80>
    ServerName www.example2.com
    ServerAlias example2.com
    ...
    DocumentRoot /var/www/example2.com/public_html
</VirtualHost>

blog.example2.com.conf

<VirtualHost *:80>
    ServerName blog.example2.com <-- redirects back to https://www.example.com
    ...
    DocumentRoot /var/www/blog.example2.com/public_html
</VirtualHost>

Best Answer

To answer your immediate questions:

  1. Is it possible to have subdomains for two sites on one IP? Or can you only have subdomains for the default?

yes, you can have multiple subdomains and top level domains on one IP. Your vhosts, as posted, appear configured correctly.

  1. Is there any way to get subdomain on example2 to work?

I suspect the redirect is occurring either because of apache not seeing/loading the blog.example2.com vhost file or a Wordpress config issue:

  • Apache will skip loading any invalid vhost files, and fall-back to domain defined in the 000-default.conf . If you are using Apache2, you may need to check the /etc/apache2/sites-enabled folder to make sure blog.example2.com.conf is enabled and being loaded. Also, don't forget to restart Apache if you've modified or added a vhost file.

  • The Wordpress site as initially setup reflects a domain of "example.com". Wordpress saves absolute urls for its GUIDs in the wp_posts table and for the site's domain (url) and homepage url in the wp_options table of its database. You may need to do a find and replace to update the domain in the database. This can be done directly via SQL query or using the WP-CLI tool. Both methods are in the answers to https://wordpress.stackexchange.com/questions/7693/what-sql-query-to-do-a-simple-find-and-replace

  • Sometimes, developers may override and set the the top level domain (site url) and homepage url in wp-config.php. The two settings would resemble:

    define('WP_HOME', 'http://www.example.com'); define('WP_SITEURL', 'http://www.example.com');

  • The Wordpress site's .htaccess file has a redirect rule pointing to www.example.com

One final note- make sure your vhost for the Wordpress site/subdomain allows for .htaccess. There should be a block similar to:

<Directory "/var/www/blog.example2.com/public_html">
    Options FollowSymLinks
    AllowOverride All

    Order allow,deny
    Allow from all
</Directory>
Related Topic