Virtualhost For Wildcard and Static Subdomain - apache

This is what I basically have now:
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName app1.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
ServerAlias *.example.com
</VirtualHost>
This configuration makes app1.example.com a default serving VirtualHost.
Thus, when another.domain.com is pointed to *.example.com, it is served by app1.example.com.
How this configuration can be changed, so that another.domain.com would be served by *.example.com, still having app1.example.com, *.example.com and example.com working too?
Thank you.
P.S. I'm basing somewhat my question on this SO answer

My own research gave me the only workaround so far. I've ended up using another port for app1.example.com.
<VirtualHost *:8080>
DocumentRoot /var/www/app1
ServerName app1.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/example
ServerName example.com
ServerAlias *.example.com
</VirtualHost>

Related

httpd VirtualHost overwrite another VirtualHost

I have a problem whereby my VirtualHost is overriding another VirtualHost on the same domain.
httpd.conf
IncludeOptional conf.d/*.conf
example.conf
<VirtualHost *:80>
DocumentRoot /var/www/html/example/
ServerName www.example.com
ServerAlias www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/html/example/
SSLEngine on
SSLCertificateFile /etc/httpd/ssl/example/example.crt
SSLCertificateKeyFile /etc/httpd/ssl/example/example.key
SSLCertificateChainFile /etc/httpd/ssl/example/chain.cer
ServerName www.example.com
ServerAlias www.example.com
</VirtualHost>
get.example.conf
<VirtualHost *:80>
DocumentRoot /var/www/html/get.example/
ServerName get.example.com
ServerAlias get.example.com
</VirtualHost>
When I created get.example.conf and when I accessed example.com, it is showing the content of get.example.com
Does anyone knows what is going wrong with my configuration?
Found out that I need to include the non-www for the VirtualHost as well. It works if I entered www.example.com but not example.com.
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
You just have to set up de ServerAlias.
In your example, it should be enough to do as follow:
<VirtualHost *:80>
DocumentRoot /var/www/html/example/
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
Note that ServerName doesn't has www., so that it's gonna match http://example.com, while with the ServerAlias it's gonna match http://www.example.com
Try to change the order of ServerName and ServerAlias to your get.example.conf:
<VirtualHost *:80>
ServerName get.example.com
ServerAlias get.example.com
DocumentRoot /var/www/html/get.example/
</VirtualHost>

Apache virtual hosts not working when using subdomain

I am trying to setup a virtual host. I have done this many times before on different servers with no problem, however, I have never tried it using a subdomain.
For some strange reason when I go to the subdomain: sub.mysite.co.uk I get redirected to the /var/www/mysite directory and not /var/www/other-site
Here is my apache.conf file:
Include /etc/apache2/sites-enabled/
NameVirtualHost *:80
<ifmodule mod_ssl.c>
NameVirtualHost *:443
</ifmodule>
<VirtualHost *:80>
ServerName *.mysite.co.uk
DocumentRoot "/var/www/newsletters/"
</VirtualHost>
And my sites-enabled file:
<VirtualHost *:80>
ServerName mysite.co.uk
DocumentRoot /var/www/newsletters
ServerAlias sub.mysite.co.uk
</VirtualHost>
Have I missed something?
Did you check your DNS entries? Maybe it's redirecting all subdomains to mysite.co.uk.
Write this in httpd.conf
Include /etc/apache2/sites-enabled/*.conf
Listen *:80
Write this in custom-vhost.conf located in sites-enabled
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin contact#example.com
DocumentRoot /var/www/domain1
ServerName domain1.me
ServerAlias www.domain1.me
</VirtualHost>
<VirtualHost *:80>
ServerAdmin contact#example.com
DocumentRoot /var/www/domain2
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>
Hope it works

Apache default host

I have Apache2, it's document root structure is:
/var/www/html
---index.html
---example.com
------/index.html
I want all requests to my-site.com to point to example.com/index.html and all other requests to be served from /var/www/html.
Here's my config:
<VirtualHost _default_:80>
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName my-site.com
DocumentRoot /var/www/html/example.com
</VirtualHost>
But all requests are served from Apache document root.
Thanks for any help.
You need a NameVirtualHost directive somewhere in your config before the second virtual host.
<VirtualHost _default_:80>
DocumentRoot /var/www/html
</VirtualHost>
NameVirtualHost *:80
<VirtualHost *:80>
ServerName my-site.com
DocumentRoot /var/www/html/example.com
</VirtualHost>

Apache subdomain from only one domain

I have a problem configuring my apache2 server
what i have:
all domains bla.org .net .com are routed to the server
what i want is:
all this access should be routed to one directory
EXCEPT dev.bla.org
this one should be routed to another directory, but only for .org tld
my configuration looks like:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/dir1
...
</VirtualHost>
<VirtualHost *:80>
ServerName dev.bla.org
ServerAlias dev
DocumentRoot /var/dir2
...
</VirtualHost>
But it doesn't do what I want. It also routes dev.bla.com etc to dir2.
How can i restrict it to .org?
<VirtualHost *.org:80> Doesn't work either.
thanks in advance
<VirtualHost *:80>
ServerAdmin webmaster#bla.net
ServerAlias bla.net *.bla.net bla.org
DocumentRoot /var/dir1
...
</VirtualHost>
<VirtualHost *:80>
ServerName dev.bla.org
ServerAlias dev.bla.org
DocumentRoot /var/dir2
...
</VirtualHost>

Virtualhost For Wildcard Subdomain and Static Subdomain

I have an odd situation where I want to have the URLs app1.example.com, example.com and *.example.com all using a different virtual host. This is what I have (excluding example.com because it just makes it messier).
<VirtualHost *>
ServerName app1.example.com
ServerAlias app1.example.com
DocumentRoot = /var/www/app1
# Other configuration for this app here
</VirtualHost>
<VirtualHost *>
ServerName wildcard.example.com
ServerAlias *.example.com
DocumentRoot = /var/www/wildcard
# other configuration for this app here
</VirtualHost>
The problem is that they conflict. Whichever one is listed first wins out. How can I host both a wildcard virtualhost and a specific one?
Note: I'm not just changing DocumentRoot in the config, so using mod_rewrite to change the DocumentRoot variable does not fix it.
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName app1.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>
Should work. The first entry will become the default if you don't get an explicit match. So if you had app.otherexample.com point to it, it would be caught be app1.example.com.
Wildcards can only be used in the ServerAlias rather than the ServerName. Something which had me stumped.
For your use case, the following should suffice
<VirtualHost *:80>
ServerAlias *.example.com
VirtualDocumentRoot /var/www/%1/
</VirtualHost>
This also works for https needed a solution to making project directories this was it. because chrome doesn't like non ssl anymore used free ssl. Notice: My Web Server is Wamp64 on Windows 10 so I wouldn't use this config because of variables unless your using wamp.
<VirtualHost *:443>
ServerAdmin test#test.com
ServerName test.com
ServerAlias *.test.com
SSLEngine On
SSLCertificateFile "conf/key/certificatecom.crt"
SSLCertificateKeyFile "conf/key/privatecom.key"
VirtualDocumentRoot "${INSTALL_DIR}/www/subdomains/%1/"
DocumentRoot "${INSTALL_DIR}/www/subdomains"
<Directory "${INSTALL_DIR}/www/subdomains/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>