Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a folder full of sites: /var/www

I have a domain: example.com

I want to write a rule in my httpd.conf that will setup an alias for each site in /var/www.

For example: /var/www/hello/public can be accessed via example.com/hello.

At the moment I am just writing my alias definitions manually, for each site: Alias /hello "/var/www/hello/public/"

share|improve this question
    
Why wouldn't you just make /var/www your DocumentRoot? –  Shane Madden Mar 6 '12 at 22:36
    
Because the routing is to the public folder, not the root - /hello = /var/www/hello/public. –  Oliver Joseph Ash Mar 6 '12 at 22:43
    
Ahh yeah, I missed that. In that case, Gabor's answer is the way to go. –  Shane Madden Mar 6 '12 at 22:44

2 Answers 2

Easiest solution would be to use mod_vhost_alias instead. It does not do exactly what you described, but it's very close.

  1. Enable vhost_alias_module using whatever technique your distribution prefers (a2enmod vhost_alias on Debian-based distros).
  2. Add these directives to httpd.conf:

    UseCanonicalName Off
    VirtualDocumentRoot /var/www/%1/public
    
  3. Create a wildcard DNS entry for example.com so any subdomain of example.com is directed to your server.

Now accessing "hello.example.com" will load the site located in /var/www/hello/public.

Alternatively, as mentioned by Gabor, you can use mod_rewrite. The solution I'm going to outline assumes that you have no other content under example.com.

  1. Enable mod_rewrite using whatever mechanism your distro prefers (a2enmod rewrite on Debian-based distros).
  2. Add these directives to the VirtualHost for example.com:

    RewriteEngine On
    RewriteRule ^([^/]+)(.*) /var/www/$1/public/$2
    

This will rewrite a request for "example.com/hello/steve" to /var/www/hello/public/steve.

share|improve this answer

Create a vhost for your domain and webroot. Write a rewrite rule instead of aliasing.

share|improve this answer
1  
What would the RewriteRule look like in this case? –  Oliver Joseph Ash Mar 6 '12 at 22:44
    
Well, a rewrite map can work here, but it's likely not quite as performant as the other solution. –  0xC0000022L Mar 6 '12 at 22:50
    
It would be useful to at least outline what the rewrite rule would look like. –  pjmorse Mar 7 '12 at 14:03
    
I've outlined one below. –  Insyte Mar 7 '12 at 22:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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