1

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/"

Oliver Joseph Ash
  • 361
  • 3
  • 5
  • 14

2 Answers2

3

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.

Insyte
  • 9,394
  • 3
  • 28
  • 45
0

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

Lucas Kauffman
  • 16,880
  • 9
  • 58
  • 93
Gabor Vincze
  • 554
  • 1
  • 4
  • 11