Subdomain Multisite goes to root even with vhost

June 9, 2017 178 views
Apache Ubuntu 16.04

I created a vhost that would direct my subdomain.example.com to the same folder that example.com resides for a WordPress multisite install but whenever I type in subdomain.example.com it goes to the Apache root.

<IfModule mod_ssl.c>
<VirtualHost *:443>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin webmaster@localhost
        ServerName subdomain.example.com
        ServerAlias www.subdomain.example.com


        DocumentRoot /var/www/example.com
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/www/example.com>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                allow from all
        </Directory>

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Is this vhost setup incorrectly?

2 Answers

For some reason after I added this to my domain's htaccess file

# add www if not subdomain
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# remove www if subdomain
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.[^.]+\.[^.]+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

It started working

  • @tannerchung

    If you're using WordPress, there's a specific set of rewrites provided during the network setup and configuration, which you'd need to use. Generally, it's recommended that you use what is provided over custom rewrites, unless you have a unique setup that requires something else.

@jtittle I had to add those rewrites in order to make the subdomain remove www and the domain to add www. The rewrites that WP provides when installing a multi site is there in addition to what I inserted.

  • @jtittle I did a completely fresh reinstall of everything... didn't need to add the htaccess rewrites this time around. I must have been noobing around and caused some issues earlier.

Have another answer? Share your knowledge.