5

Currently, I have a VirtualHost defined like so:

<VirtualHost *:80>
    ServerName mydomain.com
    ServerAlias otherdomain.com
    DocumentRoot /var/www/project/
</VirtualHost>

How can I make an Alias that only affects the otherdomain.com domain:

Alias /sub /var/www/other

So that:

http://otherdomain.com/sub -> /var/www/other
http://mydomain.com/sub -> /var/www/project/sub

The VirtualHost in question is not that simple in reality, so I'd rather not make separate VirtualHosts just for this. Are there any conditional expressions or similar that I can use inside the VirtualHost? Something along the lines of:

<VirtualHost *:80>
    ...
    <If ServerName=otherdomain.com>
        Alias /sub /var/www/other
    </If>
</VirtualHost>
Tatu Ulmanen
  • 161
  • 1
  • 7
  • Can you clarify what your configuration needs are? From what you've provided, it seems like a poster child for separate vhosts.. but what you're looking for can probably be done with mod_rewrite and a single vhost if needed. – Shane Madden Aug 14 '12 at 06:07

3 Answers3

5

No you cannot use an alias in a If.

If "%{HTTP_HOST}

is not compatible with Alias, using it will result with Apache not starting and outputting the error message:

Alias not allowed here

You should create another VirtualHost with the corresponding name and configured with your alias.

Julien Ricard
  • 151
  • 1
  • 3
  • Spent several hours googling and trying out before finding this comment. It should be more visible! Can you point to somewhere in the Apache docs where this is mentioned, because I couldn't find it anywhere. – Mike Pierce Oct 15 '18 at 22:14
1

Here are the relevant bits of the Apache documentation:

http://httpd.apache.org/docs/trunk/mod/core.html#if

and

http://httpd.apache.org/docs/trunk/expr.html

In that case, something like this should work:

<If "%{HTTP_HOST} == 'example.com'">
    Alias /sub /var/www/other
</If>

I believe you will need Apache 2.2 or greater for the "If" functionality.

cjc
  • 24,533
  • 2
  • 49
  • 69
0

I think i've found a way around.

<Directory /var/www/other>
  Require all denied
  <If "%{HTTP_HOST} == 'example.com'>
    Require all granted
  </If>
</Directory>
Alias /sub /var/www/other

<If> + Require in .htaccess file inside /var/www/other should work too.