Virtual Host points to another Virtual Host - apache

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>

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

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

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.

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>

htaccess Domain Simulation

I have a server that hosts stuff automatically from /var/www. I copied a directory like domain.com inside /var/www. I then added domain.com into my /etc/hosts for 127.0.0.1 (localhost/loopback). What's the .htaccess trick with Apache so that I can hit my site with: http://domain.com/ and it automatically knows to look in /var/www/domain.com/ (without redirection of the URL)?
You need a virtual host like this:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot /var/www/domain.com/
ServerName domain.com
</VirtualHost>
More information on the Apache documentation: http://httpd.apache.org/docs/2.0/vhosts/examples.html
I think you are looking for:
<VirtualHost *:80>
ServerAdmin mail#domain.com
DocumentRoot /var/www/htdocs/domain.com/
ServerName *.domain.com
ErrorLog logs/domain-error_log
CustomLog logs/domain-access_log common
</VirtualHost>
this will look for anything comming in on *:80 and if it's domain.com its DocRoot becomes /var/www/htdocs/domain.com