virtualhost multiple sites, apache linux server - apache

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>

Related

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

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

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?

Apache: using vhosts

I've added a new entry to vhosts, d3test. When I go to d3test/ in Google Chrome, the page isn't found, Oops! Google Chrome could not find d3test.
All of my other entries work fine, for example graphgram/ shows the correct site.
Here is my vhosts:
#
# Use name-based virtual hosting.
#
NameVirtualHost 127.0.0.1:80
<VirtualHost 127.0.0.1:80>
DocumentRoot /Users/donald/Projects/graphgram
ServerName graphgram
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot /Users/donald/Projects/lookgram
ServerName lookgram
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot /Users/donald/Projects/d3test
ServerName d3test
</VirtualHost>
Why would all entries work except the last one?
have you added that entry to /etc/hosts aswell? just in case make sure. And restart your service after that.. should work, check what the log says...

Virtual Host points to another Virtual Host

HELP! I just setup a virtual host for two sites that have a lot of traffic and I think I just messed something up! Here is the end of my httpd.conf:
NameVirtualHost *
<VirtualHost *>
ServerName www.mydomain.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *>
ServerName www.mydomain2.com
DocumentRoot /var/www/downloadr
</VirtualHost>
<VirtualHost *>
ServerName mydomain2.com
DocumentRoot /var/www/downloadr
</VirtualHost>
I added the last virtual host to solve the problem of mydomain2.com going to wwww.mydomain.com. HOWEVER, what has happened now is that www.mydomain2.com goes to www.mydomain.com.
Please help!!!
Thanks all
UPDATE
STUPIDITY beyond words - managed to copy one site to two directories and hence the 2 domains pointing to the same place!! OMG this will not happen again. Double check and recheck and recheck and recheck and recheck and recheck........
Btw, why would someone neg rep me for this?
Instead of adding the third virtual host, add
ServerAlias mydomain2.com
to the second one. So your entire configuration would be basically this:
NameVirtualHost *
<VirtualHost *>
ServerName www.mydomain.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *>
ServerName www.mydomain2.com
ServerAlias mydomain2.com
DocumentRoot /var/www/downloadr
</VirtualHost>
If you want requests for mydomain.com to actually be redirected to www.mydomain.com, so that the user sees the URL change in his/her browser, that can be done with mod_rewrite (but that's the subject of another question, search for it if you like)
I was also having problems with this it turns out that for ServerName Apache did not like the www prepended. So it should look like this:
<VirtualHost *>
ServerName mydomain2.com
ServerAlias www.mydomain2.com *.mydomain.com
DocumentRoot "c:/wamp/www" #WAMP INSTALL
</VirtualHost>