4

I m working on a web project and there a requirement that it works good on top domain, and on a domain+directory paths like this:

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

The project deals with well with include files,

But i fail to setup my VHosts to test the second case www.myDomain.com/somedir/myproject/

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>

.htaccess

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]

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

1
  • You can't do this this way. Virtualhosts are based on servername, and not on servname + directory. Apr 9, 2013 at 11:20

1 Answer 1

8

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.

3
  • thanks! I had also to update the .htaccess rules to RewriteRule /somedir/myproject/index.php [NC,L]
    – d.raev
    Apr 12, 2013 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. Apr 12, 2013 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, 2013 at 7:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .