2

I have various virtual hosts (vhosts) setup on an Apache 2.2 server such as:

examplea.domain
exampleb.domain
testsitea.domain
testsiteb.domain
testsite.domain
othersitea.domain

On virtual hosts matching the regular expression "site[a-z].domain", I want to be able to serve the same file (say logo.gif) from /usr/local/apache/files/logo.gif .

If I wanted to do it on each site, I would obviously have to have something like:

<VirtualHost *:80>
ServerName testsitea.domain
...
Alias /logo.gif /usr/local/apache/files/logo.gif
...
</VirtualHost>

<VirtualHost *:80>
ServerName testsiteb.domain
...
Alias /logo.gif /usr/local/apache/files/logo.gif
...
</VirtualHost>

etc etc

But, for various reasons, I cannot ensure the VirtualHost will be setup with the alias in place and therefore I need to make it a global Apache setting, but with regular expression matching on the ServerName.

Can anyone point me in the correct direction if this is at all possible?

Richy B.
  • 213
  • 1
  • 6

1 Answers1

0

I have a suggestion, I don't know if it's ok for your environment but it can give you idea. I don't know how to do that with Alias but maybe you can use mod_rewrite, to do the same things. First of all create a site like common.domain this virtual host will be use to store all files use by others sites.

Create a Global rewrite rule to perform redirection ,thread on stackoverflow . You can define this rewrite rule in a Directory section , example if all your vhosts are store under directory /var/www/vhosts like this: * /var/www/vhosts/siteA.domain/

  • /var/www/vhosts/siteB.domain/

  • /var/www/vhosts/siteC.domain/

  • /var/www/vhosts/siteD.domain/

You can define the the rewriterule like that :

<Directory /var/www/vhosts>
                Options FollowSymLinks
                AllowOverride None
                RewriteEngine On
                RewriteCond "%{HTTP_HOST}" "site[a-z]*\.example\.com" [NC,OR]
                RewriteCond "%{SERVER_NAME}" "site[a-z]*\.example\.com" [NC]
                RewriteRule "/logo.gif"  "http://common.example.com/logo.gif" [L]

# example:
# RewriteRule "/logo.gif" "http://alvita.a.l.pic.centerblog.net/6ba61946.gif" [L]

</Directory>

So you have Virtual Host matching , and you can do wathever you want , and you can apply this solution for many web server.

I think it's the best solution or you can define the global rewrite rule in the httpd.conf outside the parent directory . The problem with this solution you will need to add this in each virtualhost :

RewriteOptions inherit

Because by default rewrite rule are not apply in VirtualHost. You have more information on the stakoverflow thread

Have a nice day.

Xerus
  • 506
  • 4
  • 3