Apache Virtual Host using mod_vhost_alias - apache

I am trying to set up my apache module to dynamically direct all requests to a specific folder and then match the name to a folder of the same name.
To do this I set the following in my 000-default.conf file in the sites-available folder.
UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2
This worked great.
Then I wanted to setup a couple of different domains to not point to the example folder, but somewhere else, so I added a couple of these before the VirtualDocumentRoot line:
<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>
However, now the dynamic pointing does not work anymore and all the URL's are redirected to the first -> VirtualDocumentRoot location.
Can someone please indicate to me what I am doing wrong?
Full Code Example In apache2/sites-available/000-default.conf:
<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub2.example.com
VirtualDocumentRoot /var/www/sub2.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub3.example.com
VirtualDocumentRoot /var/www/sub3.example.com
</VirtualHost>
UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2

Do not use VirtualDocumentRoot for simple Virtualhosts, use only DocumentRoot.
VirtualDocumentRoot defines the mass-virtualhost catch-all, and by definition you can only have one mass-virtualhost (else how could apache knows which VH a given hostname should match).
Edit:
Now you need some other changes:
- ensure you have NameVirtualHost *:80 somewhere in apache configuration (unless you use Apache 2.4).
- Move the Mass-Virtualhost as first, so it will become the default virtualhost. The default virtualhost is used when the request host name does not match any ServerName directive. (You can check the default VH by running apache with -S option).

I have figured out how to do this, and decided to post the solution here for anyone else sitting with a similar problem:
SO to setup apache2, using mod_vhost_alias to have all domains point to a generic folder with the same name, but specific domains to point elsewhere, this is what you need to do.
In your 000-default.conf site config file, write the following code:
UseCanonicalName Off
Then add the following block for each specific domain you want to point to a specific folder, replacing example.com with your domain name:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.*
DocumentRoot path/to/your/folder
</VirtualHost>
Then add the next block to point all other generic domains to a generic folder:
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias www.*
VirtualDocumentRoot path/to/your/folder/%2+
</VirtualHost>
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias *
VirtualDocumentRoot path/to/your/folder/%1+
</VirtualHost>
The first block will direct all domains, starting with www. to a folder matching the name after the www.
The second block is to direct the same domains, when no www. is specified, to the same folder.
For more information on the dynamic mass virtual host options to use in the document root, go to: http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

Related

How do I set the entered subfolder as root?

I have setup VirtualHost and it works good when I use the domain (ex. www.website_1.se). The problem is that the DocumentRoot that is set for the VirtualHost is not applied when I visit the website through localhost (ex. localhost/website_1.se).
My root folder looks like this:
website_1.se, website_2.se, website_3.se
And my conf:
<VirtualHost *:80>
ServerAdmin admin#website_1.se
ServerName website_1.se
ServerAlias www.website_1.se
DocumentRoot "C:/xampp/htdocs/website_1.se"
</VirtualHost>
Inside website_1.se, I have html-files with src attributes, some of them starts with '/', which refers to the root.
So if I have a src="/images/file.jpg" inside the index file of "website_1.se", and enter "localhost/website_1.se, Apache will try to load that image from localhost/images/file.jpg instead of localhost/website_1.se/images/file.jpg
So my question is. How can I use '/' in paths, so that it works the same way for both localhost and VirtualHost (domain)?
EDIT (I ask the question again, better and clearer this time hopefully.)
I need to be able to host multiple websites on my apache server.
I have managed to do it with the help of vhost.
For example:
<VirtualHost *:80>
ServerName site_1.se
ServerAlias www.site_1.se
DocumentRoot "C:/xampp/htdocs/site_1.se"
</VirtualHost>
<VirtualHost *:80>
ServerName site_2.se
ServerAlias www.site_2.se
DocumentRoot "C:/xampp/htdocs/site_2.se"
</VirtualHost>
So when I enter www.site_1.se I get to localhost(htdocs)/site_1.se. And when I enter www.site_2.se I get to localhost(htdocs)/site_2.se and so on. Perfect, that's the point.
Now here is the problem. These folders (site_1.se & site_2.se) contains html-files, and in these files I have links with addresses that starts with '/', which refers to the root.
So for example if have: <img src="/images/file.png">
inside a html-file in site_1.se, Apache tries to locate the image in:
localhost/site_1.se/images/file.png (if entering www.site_1.se)
or
localhost/images/file.png (if entering localhost/site_1.se)
So this obviously becomes a problem when developing in localhost and using '/' at the beginning of paths. Because when you then visit the page from the domain name, the links are wrong.
So I'm wondering how can I set up apache, so I can specify paths starting with '/', and get the same root no matter how I visit the site?
for localhost configuration listen on different ports so as to distinguish between the different sites. You need to tell Apache to listen on those ports also.
Listen 80
Listen 81
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/site_1.se"
Require all granted
</VirtualHost>
<VirtualHost *:81>
DocumentRoot "C:/xampp/htdocs/site_2.se"
Require all granted
</VirtualHost>

APACHE VHost - How to check multiple folders to check if the first one fails

I have the following Virtual Host config block which works.
<Virtualhost *:80>
VirtualDocumentRoot "/www/sites/%1/wwwroot"
# VirtualDocumentRoot "/www/sites/%1/www"
# VirtualDocumentRoot "/www/sites/%1/public"
# VirtualDocumentRoot "/www/sites/%1/wwwroot/public"
# VirtualDocumentRoot "/www/sites/%1/"
ServerName sites.dev
ServerAlias *.dev
ServerAlias *.dev.*.xip.io
UseCanonicalName Off
</Virtualhost>
Checking out the # tags in the Vhost I have tried adding these and the fail. I have tried adding another VHost block with only the VDocRoot path changed and this is not working. Any help anyone can provide would be amazing!
UPDATE:
I should mention this is for my local dev machine (mac os x)
You can't just list multiple DocumentRoot or VirtualDocumentRoot.
Here's a recipe for mod_rewrite:
http://httpd.apache.org/docs/2.2/rewrite/remapping.html#multipledirs

How to configure dynamic subdomains for Apache2 on Ubuntu?

I need all url mydomain.com, a.mydomain.com, b.mydomain.com, whatever.mydomain.com....
point to the same DocumentRoot, the subdomain is dynamic(maybe have more than hundreds)
Now I have the following lines in 000-default.conf:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias *.mydomain.com
The mydomain.com is work, but all subdomain is not found.
Can someone help me? thanks so much.....
For example:
A user register a new account, the new account is "obama" then the url would be "obama.mydoamin.com". The subdomain can be entry when the account create immediately.
Wildcard sub-domains are possible using Apache virtual hosts.
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName xyz1.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/wildcard
ServerName other.example.com
ServerAlias *.example.com
</VirtualHost>
The first entry will become the default if you don't get an explicit match. So if you had xyz.otherexample.com point to it, it would be caught be xyz1.example.com. You need to turn on the name based virtual hosts with the first entry.
For further details you can also refers to apache documentation apache Doc

Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?

This is probably an easy question, but I want to understand better how Apache works with virtual hosts. I am setting up virtual hosts because I work on multiple websites at once and I don't want to use subdirectories. I was pretty much using the default Apache httpd.conf file with the DocumentRoot pointing to something like "/www". I uncommented the virtual hosts include and added the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Now when I go to http://localhost I get the default page for site1.
I'm sure there is a reason why this makes sense, but I don't quite understand it. I would've thought that only requests that were explicitly to http://site1.test would get routed through that directive and it wouldn't just become the default. Can someone explain why it becomes the default.
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
(Should be true for 2.x also)
"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."
answer 1 is correct
and i'd add with namevirtualhosts as the first entry
essentially matches any not-named elsewhere virtualhost
it should ONLY be used to catch unintentional mal-formed and broken traffic
ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a directory readable by apache with only a robots.txt denying everything
ServerName bogus
ErrorDocument 404 "/errordocuments/unknown-name.html"
#custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
ErrorLog /var/log/httpd/unknown-error.log
CustomLog /var/log/httpd/unknown-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a possibly different directory readable by apache with only a robots.txt denying everything
ServerName www.xxx.yyy.zzz
ServerAlias john.domain.com
ErrorDocument 404 "/errordocuments/ip-name.html"
ErrorDocument 403 "/errordocuments/ip-name.html"
#custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
ErrorLog /var/log/httpd/ip-error.log
CustomLog /var/log/httpd/ip-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
RedirectPermanent / http://www.domain.com/
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain
ServerName www.domain.com
ServerPath /domain
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
RedirectPermanent / http://www.domain2.com/
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain2
ServerName www.domain2.com
ServerPath /domain2
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.
http://httpd.apache.org/docs/2.2/vhosts/details.html
"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"
You can always add this code below, put it right below NameVirtualHost *:80 so that your default document root is served by default if no other virtual hosts found.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /my/default/document/root
</VirtualHost>
Simply put this code at top in httpd-vhosts.conf
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot d:/xampp/htdocs
<Directory "d:/xampp/htdocs/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
One way to do this is:
In your VirtualHosts configuration, enter the specific local site name you want to enable instead of using a wildcard:
<VirtualHost site1.dev:80> instead of <VirtualHost *:80>
Switch off NameVirtualHost *:80 which can be done by commenting it out in your vhosts.conf file
In your /etc/hosts file mention both aliases for the loopback IP:
127.0.0.1 localhost site1.dev
That's it. You should see that localhost goes to the default DocumentRoot as usual and the site1.dev goes to the site you've setup as virtual host.

Apache Multiple VirtualDocumentRoot

Using Apache2 on a Linux system is there a way to have multiple VirtualDocumentRoot using mod_vhost_alias?
This is naming convention I am currently using and would like to continue to use:
host directory
127.0.0.1 domain domain.com
127.0.0.1 sub.domain domain.com_sub
Then in my vhosts section of the httpd.conf I have:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
VirtualDocumentRoot /var/www/%0.0.com
</VirtualHost>
<VirtualHost 127.0.0.1>
VirtualDocumentRoot /var/www/%2.0.com_%1
</VirtualHost>
The problem with this is when I visit sub.domain the Apache error log shows that it is looking for /var/www/sub.domain.com rather than /var/www/domain.com_test which leads me to believe it only reads the first rule and then fails, but what I would like it to do is use any document root that satisfies either of the two VirtualDocumentRoot rules.
Apache typically will pick the first virtual host whose ServerName or ServerAlias matches the host name provided in the Host HTTP header. In your case, since you have no ServerName directives, Apache supposedly uses a reverse DNS lookup on the IP address to fake a server name, and presuming that the reverse DNS leads to domain.com, which doesn't match, Apache then defaults to the first virtual host. Sounds complicated, I know... the bottom line is, you should use ServerName and ServerAlias to make the configuration explicit. Try something more like this:
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
ServerName domain.com
ServerAlias www.domain.com
VirtualDocumentRoot /var/www/%0
</VirtualHost>
<VirtualHost 127.0.0.1>
ServerName sub.domain.com
ServerAlias *.domain.com
VirtualDocumentRoot /var/www/%2.%3_%1
</VirtualHost>
That should use /var/www/domain.com for http://domain.com and /var/www/www.domain.com for http://www.domain.com, both of which are served by the first vhost, and /var/www/sub.domain.com for http://sub.domain.com, /var/www/blah.domain.com for http://blah.domain.com, and so on.
You have to qualify the backreferences when you want to put a '.' in the file path. So you need to have it like this:
VirtualDocumentRoot /var/www/%2.0.%3_%1
Regarding the OP and the issue with "/var/www/html" being set:
The problem I had to this was using %1 instead of %2. Here's my working example:
ServerAlias www.*.org.au
UseCanonicalName Off
VirtualDocumentRoot /path/to/sites/%2/pub
Hope that helps someone!
I read the docs on "Directory Name Interpolation" in mod_vhost_alias docs.
I finally found a configuration that allows flexible subdomain creation.
See apache docs on mod_vhost_alias
If your root dev domain has 3 parts like dev.example.com you can use %-4+ as a placeholder for everything before the root domain. If it has 4 parts, use %-5+.
<VirtualHost *:80>
VirtualDocumentRoot "/var/www/%-4+/webroot"
ServerName www.dev.example.com
ServerAlias *.dev.example.com
php_admin_value auto_prepend_file /var/www/setdocroot.php
</VirtualHost>
This way you can create a directory named /var/www/sub.domain/webroot and access it with the url sub.domain.dev.example.com.
The line php_admin_value auto_prepend_file /var/www/setdocroot.php fixes the docroot on some systems like OSX 10.9+
Here is the content of setdocroot.php :
<?php
$_SERVER['DOCUMENT_ROOT'] = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['SCRIPT_FILENAME']);
?>
What I am noticing with this configuration is that $_SERVER['DOCUMENT_ROOT'] is pointing to /var/www/html and not to the vhost's doc root.
weird.
Update (2010-07-24):
I just wrote a blog post how to setup your http proof server
http://www.devcha.com/2010/07/how-to-setup-your-http-proof-server.html