htaccess Domain Simulation - apache

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

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

redirecting url from one subfolder to another using conf file / ubuntu

I have web site on domain.com
which is located at /var/www/main
I also have started another web site which I want to be on url domain.com/a/b
But the actual file location is /var/www/main/a/b/c
Problem is that I want the users to use url domain.com/a/b and get files from /var/www/main/a/b/c
So I've found domains conf file at /etc/apache2/sites-available/domain.com.conf
It had only this
<virtualhost *:80>
ServerName domain.com
DocumentRoot /var/www/main
</virtualhost>
Now after reading different topics about it I've tried to add this so now my domain.com.conf file looks like this
<virtualhost *:80>
ServerName domain.com
DocumentRoot /var/www/main
</virtualhost>
<virtualhost *:80>
ServerName domain.com
ServerPath /a/b/
DocumentRoot /var/www/main/a/b/c
RewriteEngine On
RewriteRule ^(/a/b/.*) /var/www/main/a/b/c
# ...
</virtualhost>
But this does not work at all.
What am I doing wrong ?
No need for Rewrite Rules. Just set up an alias in /etc/apache2/sites-available/domain.com.conf :
<virtualhost *:80>
ServerName domain.com
DocumentRoot /var/www/main
Alias /a/b /var/www/main/a/b/c
</virtualhost>
Then restart apache :
$ sudo service apache2 restart

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.

Why does http://localhost redirect to my default virtual host once I setup virtual hosts in Apache?

This is probably an easy question, but I want to understand better how Apache works with virtual hosts. I am setting up virtual hosts because I work on multiple websites at once and I don't want to use subdirectories. I was pretty much using the default Apache httpd.conf file with the DocumentRoot pointing to something like "/www". I uncommented the virtual hosts include and added the following:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName site1.dev
DocumentRoot /www/site1
</VirtualHost>
<VirtualHost *:80>
ServerName site2.dev
DocumentRoot /www/site2
</VirtualHost>
Now when I go to http://localhost I get the default page for site1.
I'm sure there is a reason why this makes sense, but I don't quite understand it. I would've thought that only requests that were explicitly to http://site1.test would get routed through that directive and it wouldn't just become the default. Can someone explain why it becomes the default.
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
(Should be true for 2.x also)
"If no matching virtual host is found, then the first listed virtual host that matches the IP address will be used.
As a consequence, the first listed virtual host is the default virtual host. The DocumentRoot from the main server will never be used when an IP address matches the NameVirtualHost directive. If you would like to have a special configuration for requests that do not match any particular virtual host, simply put that configuration in a container and list it first in the configuration file."
answer 1 is correct
and i'd add with namevirtualhosts as the first entry
essentially matches any not-named elsewhere virtualhost
it should ONLY be used to catch unintentional mal-formed and broken traffic
ie a machene with one ip called john.domain.com running www.domain.com and www.domain2.com as valid webservers on ip www.xxx.yyy.zzz might have an optimal config like thus
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a directory readable by apache with only a robots.txt denying everything
ServerName bogus
ErrorDocument 404 "/errordocuments/unknown-name.html"
#custom 404 describing how/what they might have done wrong try pointing a browser {with a hosts file at http://bogus/ on 193.120.238.109 to see mine#
ErrorLog /var/log/httpd/unknown-error.log
CustomLog /var/log/httpd/unknown-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/static-sites/unknown/
# a possibly different directory readable by apache with only a robots.txt denying everything
ServerName www.xxx.yyy.zzz
ServerAlias john.domain.com
ErrorDocument 404 "/errordocuments/ip-name.html"
ErrorDocument 403 "/errordocuments/ip-name.html"
#custom 404 telling them as a likely hacker/bot you wish to have nothing to do with them see mine at http://193.120.238.109/
ErrorLog /var/log/httpd/ip-error.log
CustomLog /var/log/httpd/ip-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain.com
RedirectPermanent / http://www.domain.com/
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain
ServerName www.domain.com
ServerPath /domain
ErrorLog logs/www.domain.com-error.log
CustomLog logs/www.domain.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
ServerName domain2.com
RedirectPermanent / http://www.domain2.com/
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/webserver/ftpusers/domain2
ServerName www.domain2.com
ServerPath /domain2
ErrorLog logs/www.domain2.com-error.log
CustomLog logs/www.domain2.com-access.log combined
</VirtualHost>
Confirming that for Apache 2.x, the first virtual host (with the same port number) will be used if a matching virtual host is not found.
http://httpd.apache.org/docs/2.2/vhosts/details.html
"If no matching vhost could be found the request is served from the first vhost with a matching port number that is on the list for the IP to which the client connected"
You can always add this code below, put it right below NameVirtualHost *:80 so that your default document root is served by default if no other virtual hosts found.
<VirtualHost *:80>
ServerName localhost
DocumentRoot /my/default/document/root
</VirtualHost>
Simply put this code at top in httpd-vhosts.conf
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot d:/xampp/htdocs
<Directory "d:/xampp/htdocs/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
One way to do this is:
In your VirtualHosts configuration, enter the specific local site name you want to enable instead of using a wildcard:
<VirtualHost site1.dev:80> instead of <VirtualHost *:80>
Switch off NameVirtualHost *:80 which can be done by commenting it out in your vhosts.conf file
In your /etc/hosts file mention both aliases for the loopback IP:
127.0.0.1 localhost site1.dev
That's it. You should see that localhost goes to the default DocumentRoot as usual and the site1.dev goes to the site you've setup as virtual host.