VirtualHost for subdomain also re-routes traffic to host domain - apache

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

Related

Apache 2.2 Cent Os Subdomain Virtual Host not working

I am trying to add domain and subdomain in apache virtual host. I already created the subdomain and pointed to my type A record. I dont see any issues over there.
Below is my httpd.conf entry: now only my root domain i am able to access, subdomain I could not access, I have gone through many links, but nothing worked for me.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName mydomain.com
DocumentRoot "/var/www/html"
</VirtualHost>
<VirtualHost *:80>
ServerName admin.mydomain.com
DocumentRoot "/var/www/admin"
</VirtualHost>
Can someone help me on this please ?

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

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

Apache 2.4 Wildcard Subdomain Virtual Host

My goal is to have the following occur, when the user visits the sites below to the left it takes them to the directories on the right:
www.example.com or example.com => /var/www/example/public_html/
*.example.com => /var/www/example/public_html/sites/%1/public_html/
I have tried the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example/public_html
</VirtualHost>
<VirtualHost *:80>
ServerAlias *.example.com
VirtualDocumentRoot /var/www/example/public_html/sites/%1/public_html
</VirtualHost>
The main domain works with or without www, but every time I try to visit any other subdomain it takes me to and gives me a 500 Internal Server Error. The URL changes to look like below when I type in a subdomain:
{subdomain}.example.com/sites/{subdomain}/public_html/
I am on Apache 2.4.7 and yes I have setup the WildCard on my DNS server. I would greatly appreciate any answers deeming that I have been slamming my head on a wall for 2 weeks trying to get this working.
Okay the whole problem was from the wildcard directory being beneath another directory that is already bound. After I made a directory elsewhere it started working like expected, go figure...
Hope this helps anyone else who happens to run into this odd bug... ^_^
I think the configuration you are looking for it's quite similar to this one:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/example/public_html
</VirtualHost>
<VirtualHost *:80>
ServerName *.example.com
ServerAlias *.example.com
DocumentRoot /var/www/wildcardexample/public_html
</VirtualHost>
What to you think?
In this case, any subdomain will be redirected to the same site. Is this the configuration you want? Or you want different configurations for each subdomain?

virtualhost multiple sites, apache linux server

Sorry if this sounds stupid, its how I feel since I've done this in the past and can't figure out whats wrong.
Anyways, I had two sites setup on my fedora linux box, now I'm trying to add a third site. However when I go to www.site3.com it gets redirected to the first site.
My VirtualHost code is very basic, please let me know what else I should be adding and any issues you can see which result to the issue I mentioned.
httpd.conf:
<VirtualHost *:80>
DocumentRoot /var/www/html/web/site1/
ServerName site.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/web/site2/
ServerName site2.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/web/site3/
ServerName site3.com
</VirtualHost>
Is there anything else I need to change other than this? The other first two sites still work fine, I've restarted the httpd service, but no avail
Thanks in advance
www.site3.com and site3.com are not the same hostname. See the ServerAlias directive.
<VirtualHost *:80>
DocumentRoot /var/www/html/web/site3/
ServerName site3.com
ServerAlias www.site3.com
</VirtualHost>