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 m working on a Zend web project and there a requirement that it works that it works good on top domain, and on a domain+directory paths like this:

www.myDomain.com
www.myDomain.com/somedir/myproject/

There is a good implementation in the project by no for figgering out the basePath and properly include files (I inherited the code from unknown but good developer);

But i fail to setup my VHosts to test the second case www.myDomain.com/somedir/myproject/ if all my includes work properly.

My current setup is as simple as possible:

#hosts
127.0.0.1       www.myDomain.com

.

#httpd-vhosts.conf
NameVirtualHost *:80
<Directory C:/Projects>
  Order Deny,Allow
  Allow from all
</Directory>

<VirtualHost *:80>
  DocumentRoot C:\wamp\www
  ServerName localhost
</VirtualHost>   

<VirtualHost *:80>
    DocumentRoot "C:\Projects\MyProject\public_html"
    ServerName  "www.myDomain.com"
</VirtualHost>

#@ToDo make this work ...
#<VirtualHost *:80>
#    DocumentRoot "C:\Projects\MyProject\public_html"
#    ServerName  "www.myDomain.com/somedir/myproject/"
#</VirtualHost>

Can i do this just with Virtual host ? What is the best practice to achieve this setup on local machine?

EDIT: The project uses Zend and Front Controller architecture (all request are redirected to index.php) and the current rewrite rule in the .htaccess is:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php [NC,L]
share|improve this question
    
You can't do this this way. Virtualhosts are based on servername, and not on servname + directory. –  Krist van Besien Apr 9 '13 at 11:20
add comment

1 Answer

up vote 2 down vote accepted

What you need is an alias, not vhost. Virtualhosts work with hostnames, not hostname + directory.

<VirtualHost *:80>
  DocumentRoot "C:\Projects\MyProject\public_html"
  ServerName  "www.myDomain.com"

  Alias /somedir/myproject  "C:\Projects\MyProject\public_html"
</VirtualHost>

Edit:

The OP's problem appears to be somewhat different, and his solution is to send everything that is not an actual physical file to index.php, using a RewriteRule in the .htaccess file. This works, but putting them in your httpd.conf (or one if it's included files) is more efficient.

There is one way you can easily move working RewriteRules from .htaccess to httpd.conf without having to change them: Use a containers.

So if you have a .htaccess in for example /var/www/myproject you can move to httpd.conf like this.

<Directory /var/www/myproject>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php [NC,L]
</directory>

The advantage over putting the rules in .htaccess files is that a .htaccess file gets read for every request. So it's better not to have any at all. You can move the RewriteRule outside of its directory context (to avoid having a internal redirect on each match) But then you would have to modify the pattern, as in a server context it's the whole URL you're matching against, not just the part after the directory.

share|improve this answer
    
thanks! I had also to update the .htaccess rules to RewriteRule /somedir/myproject/index.php [NC,L] –  d.raev Apr 12 '13 at 13:02
    
If you have access to your httpd.conf (which you appear to have) I would not put rewriterules in .htaccess files. Put them in your config. Putting rewriterules in .htacces files is a kludge that should be avoided if at all possible. –  Krist van Besien Apr 12 '13 at 15:14
    
thanks again sir, it is development environment so its fine for me if it works :) I updated the question with my current .htaccess settings, so if you wish add an working example how to make this with httpd.conf to help other people on the issue. –  d.raev Apr 15 '13 at 7:18
add comment

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.