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've got LAMP setup on a VM that I use for multiple projects. My document root is a /www and I create a folder for each project within it.

/www/project1-name/
/www/project2-name/

I access them in my browser like this:

http://dev.vm/project-name/

I'd like to move my htaccess and index files to a public folder within a project, so they match their production setup more accurately.

/www/project-name/public/

...but still access them with the same url as above. How can I get Apache configured to look for public/ folders as document roots for each of my projects?

Note: I understand I can create hosts and vhost entries for each project to have it's own document root, but I'm trying to to avoid that - because I plan on using a similar setup on my remote server to house multiple projects at a subdomain called 'demo'.

share|improve this question

3 Answers 3

up vote 2 down vote accepted

I'd go one step further with mod_alias and use the AliasMatch directive. Should work the same as EightbitTony's solution, but give you the extra regex power.

I did a bit of testing and this looks like it should do it.

AliasMatch ^/([a-zA-Z0-9_-]+)/(.*) /www/$1/public/$2
share|improve this answer
1  
Nice, hadn't realised it was there, like that solution. –  EightBitTony May 28 '12 at 21:05
    
Could you help with the regex pattern? I'm trying ([^\/]+) but it's not working. –  Atticus Finch May 28 '12 at 22:55
    
I edited the answer with what I think should work. –  kashani May 29 '12 at 4:22

I don't think you want to do any re-writing, don't you just want some good old fashioned Alias statements?

From http://httpd.apache.org/docs/2.0/mod/mod_alias.html,

Description: Maps URLs to filesystem locations
Syntax:      Alias URL-path file-path|directory-path
Context:     server config, virtual host
Status:      Base
Module:      mod_alias

So,

Alias /project-name/  /some/path/to/project-name/public/
Alias /project-name2/ /some/path/to/project-name2/public/
Alias /project-3/     /another/totally/different/path/public/
share|improve this answer
    
Is it possible to do it dynamically with some specific conditional logic? So I won't have to add a new vhosts entry for every new project. –  Atticus Finch May 28 '12 at 19:11
    
It's not a vhost, it's an alias. You only have to add one line to your apache config per project. I'm sure it is possible to use re-write to do it dynamically, but it's overkill. –  EightBitTony May 28 '12 at 19:19

Take a look at the apache dynamically configured mass virtual hosting documentation. It doesn't sound like you have huge numbers of vhost so the VirtualDocumentRoot and related configuration directives will probably do what you want e.g.

 VirtualDocumentRoot /www/%2/public

which would map to /www/project-name/public. The %n parameter is interpolated from the URL with %0 being the whole, %1 the first part etc.

share|improve this answer

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.