2

I am trying to set up an apache 2.4 server with massive subdomains using ServerAlias. The basic configuration for the main domain works just fine (main domain => 200, all subdomains => 404):

UseCanonicalName Off

<VirtualHost example.com>
        ServerName example.com
        ServerAlias www.example.com
        VirtualDocumentRoot "/var/www/vhosts/example.com/httpdocs"
        DirectoryIndex index.php index.html

        <Directory />
                Options +Indexes +FollowSymLinks +Includes
                <IfModule !mod_access_compat.c>
                        Require all granted
                </IfModule>
                <IfModule mod_access_compat.c>
                        Order allow,deny
                        Allow from all
                </IfModule>
                AllowOverride All
        </Directory>
</VirtualHost>

But when I add the lines for my generic vhost set up the web server returns an 404 status code on every vhost (subdomain) request:

UseCanonicalName Off

<VirtualHost example.com>
        ServerName example.com
        ServerAlias www.example.com
        VirtualDocumentRoot "/var/www/vhosts/example.com/httpdocs"
        DirectoryIndex index.php index.html
        ServerAlias *.example.com
        VirtualDocumentRoot "/var/www/vhosts/example.com/%1.0"
        DirectoryIndex index.php index.html

        <Directory />
                Options +Indexes +FollowSymLinks +Includes
                <IfModule !mod_access_compat.c>
                        Require all granted
                </IfModule>
                <IfModule mod_access_compat.c>
                        Order allow,deny
                        Allow from all
                </IfModule>
                AllowOverride All
        </Directory>
</VirtualHost>

I think it must have to do something with the right folder path. The folder path always looks like this:

/var/www/vhosts/maindomain.com/subdomain.maindomain.com

What I need is simply a configuration that matches the main domain and generic sub domains associated to that. So,

www.example.com
example.com

matches folder path:

/var/www/vhosts/example.com/httpdocs

And

ci.example.com
tracker.example.com
other.example.com

should match to its folder path like this:

/var/www/vhosts/example.com/ci.example.com
/var/www/vhosts/example.com/tracker.example.com
/var/www/vhosts/example.com/other.example.com

Also, for some strange reason, if I change it to %0.0 in the subdomain path, the subdomains works, but the main domain does not. So, with using that, it's the other way around. Main domain => 404, all subdomains => 200.

I found the explanation for the magic string literals for folder names in the apache 2.4 docs: http://httpd.apache.org/docs/2.4/mod/mod_vhost_alias.html But rather than solving my issue, it confuses me more and more...

The web server runs under openSUSE 42.2 minimal. As you may already suggested, the real domain name is changed here to example.com. Any help to this issue would be great.

Edit: As I said also down below in my comment to the first answer, I need a solution, that does not necessarily require any RewriteRule in the vhost config. Because I am using php systems, that do have their own (sometimes real complex) RewriteRules, so I do not want to get in trouble with that .htaccess stuff. But any other options, except cgi is possible. And yes, symlink support is enabled (or possible to enable it).


Edit:

The symlink example helped me to get the basic idea. A bit rethinking brought me to this solution below. First found rule always applies, so it worked for me:

UseCanonicalName Off
<VirtualHost *:80>
        DocumentRoot "/var/www/vhosts/example.com/httpdocs"
        ServerName www.example.com
        ServerAlias example.com
        <Directory />
                Options Indexes FollowSymLinks Includes ExecCGI
                AllowOverride All
                Require all granted
                Allow from all
        </Directory>
</VirtualHost>

<VirtualHost *:80>
        DocumentRoot "/var/www/vhosts/example.com/httpdocs"
        ServerName example.com
        ServerAlias *.example.com
        VirtualDocumentRoot "/var/www/vhosts/example.com/%1"
        <Directory />
                Options Indexes FollowSymLinks Includes ExecCGI
                AllowOverride All
                Require all granted
                Allow from all
        </Directory>
</VirtualHost>

apachectl -t and apachectl -S says that this is ok. Tested and worked! :-)

1
+50

Try

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/vhosts/example.com/httpdocs
    ServerAlias *.example.com
    VirtualDocumentRoot /var/www/vhosts/example.com/%0
</VirtualHost>

This obviously doesn't include the whole configuration but it should get your wild-card sub domains working.

Next, I would redirect all requests to www.example.com to example.com. The easiest way to do this is probably with an htaccess file inside /var/www/vhosts/example.com/www.example.com

Alturnatively, if you have symlinks on, you could symlink /var/www/vhosts/example.com/www.example.com to /var/www/vhosts/example.com/httpdocs

Edit:

You could put the redirect in the vhost configuration if you don't want to do it in .htaccess. Try this:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/vhosts/example.com/httpdocs
    ServerAlias *.example.com
    VirtualDocumentRoot /var/www/vhosts/example.com/%0
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.example.com
    RewriteRule ^/(.*)$ http://example.com/$1 [L,R=301]
</VirtualHost>

I am not aware of any way to do it without a redirct of some sort. On a side note, it is good practice to redirect non-www to www or www to non-www. Having what is essentially a duplicate of your site will be a detrement if you plan on doing any SEO work.

  • Well, thanks for that nice answer. And yes, I got symlinks enabled. Is there no solution to do it completely inside the vhost config? I would like to avoid any RewriteRule in vhost config or any additional RewriteRule in my .htaccess files in order to avoid problems with the existing ones. Because I mainly use php framework and php based systems, that require their own RewriteRules and I don't wanna to break them, because they could be really complex ones. – alpham8 Apr 25 '17 at 11:39
0

I wrote a script for myself - that works for me :)

But before reading my script - my hint to you is to not use

<VirtualHost example.com>
....
</VirtualHost>

instead use either: <VirtualHost *:80> or <VirtualHost *> to begin your VirtualHost entry.

Note: I work mainly on AIX - so I have ksh as the shell. This should work as well with either /bin/sh or /bin/bash.

Syntax:
vhost.ksh <ServrName> [ServerAlias1] [... ServerAliasN]

#!/usr/bin/ksh
# Copyright AIXTOOLS.NET, 2014
# This script creates a vhost template
# under the assumption that the site might be served by multiple hosts
# the logs are put into a sub-directory
# ${SITEroot}/logs/LOGNAME.$host
site=$1
shift
alias=$*
HOST=`hostname -s`
SITE=/var/httpd/${site}
LOGDIR=${SITE}/logs

cat -<<EOF1
# AIXTOOLS.NET vhost.ksh output
# VirtualHost template for ${site}
# SITEBASE=${SITE}
# LOGDIR=${LOGDIR}

<VirtualHost *:80>
     ServerName  ${site}
EOF1
for name in ${alias}
do
print "    ServerAlias  ${name}"
done

cat - <<EOF2

DocumentRoot "${SITE}/htdocs"

<Directory "/var/httpd/${site}/htdocs">
    Options Indexes FollowSymLinks Includes
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

#Logs for ${site}
# combined is chosen as default to support awstats reporting
    CustomLog "${LOGDIR}/access_log.${HOST}" combined
#    ErrorLog "${LOGDIR}/error_log.${HOST}"
# You can consider using different "localN" syslogs to split different vhosts
# should you need that
    ErrorLog syslog:user
#    LogLevel alert rewrite:trace3
#    LogLevel alert
#    RewriteLog "/var/httpd/${site}/logs/rewrite.log"
#    RewriteLogLevel 0
</VirtualHost>
EOF2
  • thanks for that answer. The problem is, I need multiple domains assigned to one ip. And if I understand your script right, you are genarating static ServerAlias rules based on how much arguements I paste in, right? – alpham8 Apr 28 '17 at 19:55
  • I have only one IP address, and roughly 7 domains pointing at that address. The server alias is so that those names resolve to the same virtualHost (e.g, rootvg.net is my preferred domain, rootvg.com and rootvg.org go the same virtual host. Then rewrite rules are (supposed) to rewrite to preferred domain. They don't have to - my preference! (snif - you gave the bounty to someone else. And not even one vote :p@me!) – Michael Felt May 3 '17 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.