Creating Wildcard Sub Domain Using Apache VirtualHost - apache

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.

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

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

Virtualhost For Wildcard Subdomain and Static Subdomain

I have an odd situation where I want to have the URLs app1.example.com, example.com and *.example.com all using a different virtual host. This is what I have (excluding example.com because it just makes it messier).
<VirtualHost *>
ServerName app1.example.com
ServerAlias app1.example.com
DocumentRoot = /var/www/app1
# Other configuration for this app here
</VirtualHost>
<VirtualHost *>
ServerName wildcard.example.com
ServerAlias *.example.com
DocumentRoot = /var/www/wildcard
# other configuration for this app here
</VirtualHost>
The problem is that they conflict. Whichever one is listed first wins out. How can I host both a wildcard virtualhost and a specific one?
Note: I'm not just changing DocumentRoot in the config, so using mod_rewrite to change the DocumentRoot variable does not fix it.
<VirtualHost *:80>
DocumentRoot /var/www/app1
ServerName app1.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>
Should work. The first entry will become the default if you don't get an explicit match. So if you had app.otherexample.com point to it, it would be caught be app1.example.com.
Wildcards can only be used in the ServerAlias rather than the ServerName. Something which had me stumped.
For your use case, the following should suffice
<VirtualHost *:80>
ServerAlias *.example.com
VirtualDocumentRoot /var/www/%1/
</VirtualHost>
This also works for https needed a solution to making project directories this was it. because chrome doesn't like non ssl anymore used free ssl. Notice: My Web Server is Wamp64 on Windows 10 so I wouldn't use this config because of variables unless your using wamp.
<VirtualHost *:443>
ServerAdmin test#test.com
ServerName test.com
ServerAlias *.test.com
SSLEngine On
SSLCertificateFile "conf/key/certificatecom.crt"
SSLCertificateKeyFile "conf/key/privatecom.key"
VirtualDocumentRoot "${INSTALL_DIR}/www/subdomains/%1/"
DocumentRoot "${INSTALL_DIR}/www/subdomains"
<Directory "${INSTALL_DIR}/www/subdomains/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>