How to configure dynamic subdomains for Apache2 on Ubuntu? - apache

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

Related

Name-based VirtualHosts by subfolder

I'm trying to configure Apache with three different VirtualHosts, such that a specific VirtualHost will be used when someone requests either the corresponding subdomain (e.g. foo.example.com) or the corresponding subfolder (e.g. example.com/foo).
I thought the following httpd.conf would do the trick, but the ServerAlias directives are simply being ignored:
<VirtualHost *:80 *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/http
</VirtualHost>
<VirtualHost *:80 *:443>
ServerName foo.example.com
ServerAlias example.com/foo
DocumentRoot /usr/share/web
</VirtualHost>
<VirtualHost *:80 *:443>
ServerName bar.example.com
ServerAlias example.com/bar
DocumentRoot /var/www/html
</VirtualHost>
When bar.example.com is requested the third VirtualHost is used, as intended. However, in the case of example.com/bar the first VirtualHost takes precedence despite the ServerAlias. Similarly, requesting example.com/foo matches the first VirtualHost, not the second.
How can I fix this configuration to produce the desired behavior?
ServerAlias takes a hostname, not a hostname and a path. This mechanism doesn't do what you want it to do. Just create an Alias or Redirect in the virtual host being accessed.

Apache ServerAlias not redirecting to correct ServerName

i'm try to setup multiple wordpress sites on my Amazon EC2 instance. Here's how my httpd.conf file looks like:
<VirtualHost *:80>
ServerName www.domain1.com
ServerAlias domain1.com
DocumentRoot /var/www/html/domain1
</VirtualHost>
<VirtualHost *:80>
ServerName www.domain2.co
ServerAlias domain2.co
DocumentRoot /var/www/html/domain2
</VirtualHost>
So, when i entered domain1.com or www.domain1.com in the browser, it redirects correctly to the content i wanted and so does www.domain2.co . However, when i entered domain2.co, it doesn't directs to the ServerName www.domain2.co but to the first VirtualHost settings www.domain1.com.
Anything i'm missing out here?
Try this. Apache will default to the 1st virtual host if it doesn't find a virtualhost match which means your second VirtualHost is being ignored. We use www. as an alias and the domain as the server name. See if this helps.
<VirtualHost *:80>
ServerName domain1.com
ServerAlias www.domain1.com
DocumentRoot /var/www/html/domain1
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
ServerAlias www.domain2.com
DocumentRoot /var/www/html/domain2
</VirtualHost>
Figured it out guys, the browser caches previous data when domain2.co points to domain1.com. So even when i have set the virtualhost correctly for domain2.co, the browser will still load the previous cached data from domain1.com.
Solution will be to clear browser data.
Found out that another factor affecting this could be because of your ISP.
Read more here: https://sg.godaddy.com/help/what-factors-affect-dns-propagation-time-1746

Using VirtualHosts to host multiple domains

I have searched far and wide for this answer, and can't find a working solution. So here goes,
I have 2 VirtualHosts set up on my server, with each serving a separate domain name. However, when I visit the first domain on the list, it then serves the DocumentRoot of the second domain. I even have them both listening on different ports. In my DNS under each domain I've got them leading to the IP of the server.
Here is my Apache .conf file:
ServerName 137.117.33.226
<VirtualHost *:443>
ServerName joshstroup.me
ServerAlias www.joshstroup.me
DocumentRoot /var/www/html
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/joshstroup.me.cert
SSLCertificateKeyFile /etc/apache2/ssl/joshstroup.me.key
</VirtualHost>
<VirtualHost *:80>
ServerName respice.xyz
ServerAlias www.respice.xyz
DocumentRoot /var/www/respice
</VirtualHost>

VirtualHost for subdomain also re-routes traffic to host domain

I'm trying to make it so when someone visits my site at sub.example.com, it sends them to a different folder than someone who visits example.com. Simple, yes? This is the code I use to do it:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/sub"
ServerName sub.example.com
ServerAlias sub.example.com
</VirtualHost>
I have the DNS for my domain set for a wildcard so that any subdomain still directs to my server.
The problem is, now whenever someone visits example.com or sub.example.com or any subdomain, it shows the content from /htdocs/sub
I only want it to show /htdocs/sub when sub.example.com is visited, how do I do that?
From what I've read my execution seems right, but maybe I've missed one thing that causes this to happen? I've reinstalled xampp to no avail
My main httpd.conf has this line ServerName example.com:80 if this helps
I ended up finding a solution, and to anyone in the future with the same problem, this is what I did:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot "C:/xampp/htdocs/"
</VirtualHost>
<VirtualHost *:80>
ServerName sub.example.com
DocumentRoot "C:/xampp/htdocs/sub"
</VirtualHost>
I assume that basically for the VirtualHost to differentiate between sub.example.com and example.com then a VirtualHost first must be defined for example.com before any subdomains can be configured

Creating Wildcard Sub Domain Using Apache VirtualHost

I want to have this situation :
if user request using this URL : example.com or www.example.com,
user will see index.php in this directory /home/admin1/public_html/
but when user request using other sub domain (wildcard) for example : freediscount.example.com, user will see index.php in this path : /home/admin1/public_html/userweb/freediscount.example.com
technical support on my hosting suggest me to use this method : http://www.wiredstudios.com/php-programming/setting-up-wildcard-dns-for-subdomains-on-cpanel.html
based on that tutorial, the PHP has a new job... to redirect on specific folder when user request with sub domain. I don't like this method. for me, it would be better if Apache can handle this.
nearly close to what I need is this method : Virtualhost For Wildcard Subdomain and Static Subdomain
but, I have a problem with VirtualHost setting, how to create VirtualHost correctly for that situation?
here's what I've done but didn't work :
## I think this one is for www or without www, automatically generated with WHM
<VirtualHost xx.xx.xx.xx:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /home/admin1/public_html
</VirtualHost>
## Here's what I'm trying to add
<VirtualHost xx.xx.xx.xx:80>
ServerName example.com
DocumentRoot /home/admin1/public_html/userweb/*
</VirtualHost>
Wildcard sub-domains are definitely possible using Apache virtual hosts.
I had basically the same requirements and managed to get it working with Apache's mod_vhost_alias.so module. Try this in your http-vhosts.conf file:
DocumentRoot "/home/admin1/public_html/userweb/"
<Directory "/home/admin1/public_html/userweb/">
Options None
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<VirtualHost *:80>
DocumentRoot /home/admin1/public_html/
ServerName www.example.com
</VirtualHost>
<VirtualHost *:80>
VirtualDocumentRoot /home/admin1/public_html/userweb/%1.example.com/
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /home/admin1/public_html/
ServerName example.com
</VirtualHost>
Note that I haven't tested this, but it's pretty close to the solution that worked for me.
Full details of my solution are here:
http://www.calcatraz.com/blog/wildcard-subdomains-in-apache-1422
Try with this:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /home/admin1/public_html/
ServerName www.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /home/admin1/public_html/userweb/freediscount.example.com
ServerName other.example.com
ServerAlias *.example.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /home/admin1/public_html/
ServerName example.com
</VirtualHost>
Order of virtual hosts & their specificity matters.