whatever subdomain redirect to a specific folder - apache

I have the following structure
webapp
website
.htaccess
and I have made a virtual host like the following
mydomain.com -> points to -> website
app.mydomain.com -> points to -> webapp
and I need to apply wildcard DNS concept to make any subdomain pointing to my webapp folder with keeping the subdomain in the url.
like
username.mydomain.com
company-name.mydomain.com
whatever.mydomain.com
and make them points to webapp folder
is there any virtual host or .htaccess way to do that?
Regards

According to Apache docs, the vhost resolution uses the definition order. The following config should do what you want :
<VirtualHost *:80>
ServerName www.yourdomain.com
ServerAlias yourdomain.com
DocumentRoot /www/yourdomain/website
</VirtualHost>
<VirtualHost *:80>
ServerName app.yourdomain.com
DocumentRoot /www/yourdomain/webapp
</VirtualHost>
<VirtualHost *:80>
ServerName *.yourdomain.com
DocumentRoot /www/yourdomain/customsubdomain
</VirtualHost>
Apache will route the request to the first vhost that matches the ServerName directive
yourdomain.com will match the first one
www.yourdomain.com will match the first one
app.yourdomain.com will match the second one
companyname..yourdomain.com the third one
username.yourdomain.com the third one
and so on...
*.yourdomain.com matches both app.yourdomain.com and www.yourdomain.com (but not yourdomain.com). So the wildcard (*) domain needs to be the last one in the file so it only catches request that aren't sent to www or app.

Related

How do I set the entered subfolder as root?

I have setup VirtualHost and it works good when I use the domain (ex. www.website_1.se). The problem is that the DocumentRoot that is set for the VirtualHost is not applied when I visit the website through localhost (ex. localhost/website_1.se).
My root folder looks like this:
website_1.se, website_2.se, website_3.se
And my conf:
<VirtualHost *:80>
ServerAdmin admin#website_1.se
ServerName website_1.se
ServerAlias www.website_1.se
DocumentRoot "C:/xampp/htdocs/website_1.se"
</VirtualHost>
Inside website_1.se, I have html-files with src attributes, some of them starts with '/', which refers to the root.
So if I have a src="/images/file.jpg" inside the index file of "website_1.se", and enter "localhost/website_1.se, Apache will try to load that image from localhost/images/file.jpg instead of localhost/website_1.se/images/file.jpg
So my question is. How can I use '/' in paths, so that it works the same way for both localhost and VirtualHost (domain)?
EDIT (I ask the question again, better and clearer this time hopefully.)
I need to be able to host multiple websites on my apache server.
I have managed to do it with the help of vhost.
For example:
<VirtualHost *:80>
ServerName site_1.se
ServerAlias www.site_1.se
DocumentRoot "C:/xampp/htdocs/site_1.se"
</VirtualHost>
<VirtualHost *:80>
ServerName site_2.se
ServerAlias www.site_2.se
DocumentRoot "C:/xampp/htdocs/site_2.se"
</VirtualHost>
So when I enter www.site_1.se I get to localhost(htdocs)/site_1.se. And when I enter www.site_2.se I get to localhost(htdocs)/site_2.se and so on. Perfect, that's the point.
Now here is the problem. These folders (site_1.se & site_2.se) contains html-files, and in these files I have links with addresses that starts with '/', which refers to the root.
So for example if have: <img src="/images/file.png">
inside a html-file in site_1.se, Apache tries to locate the image in:
localhost/site_1.se/images/file.png (if entering www.site_1.se)
or
localhost/images/file.png (if entering localhost/site_1.se)
So this obviously becomes a problem when developing in localhost and using '/' at the beginning of paths. Because when you then visit the page from the domain name, the links are wrong.
So I'm wondering how can I set up apache, so I can specify paths starting with '/', and get the same root no matter how I visit the site?
for localhost configuration listen on different ports so as to distinguish between the different sites. You need to tell Apache to listen on those ports also.
Listen 80
Listen 81
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs/site_1.se"
Require all granted
</VirtualHost>
<VirtualHost *:81>
DocumentRoot "C:/xampp/htdocs/site_2.se"
Require all granted
</VirtualHost>

Dynamic server aliasing in apache webserver

I have a usecase where I need to setup wildcard subdomains with condition so that for http://xyz.example.com type of request it should choose document root as /var/www/html/web and for http://xyz-portal.example.com it should choose document root as /var/www/html/admin. How this can be achieved?
Generate different virtualhosts with a default name resembling each case and then use ServerAlias for the wildcard. This is a example, adjust to your needs considering the following explanation.
Example:
<VirtualHost *:80>
ServerName www-portal.example.com
ServerAlias *-portal.example.com
DocumentRoot /var/www/html/admin
...
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
ServerAlias *.example.com
DocumentRoot /var/www/html/web
...
</VirtualHost>
Explanation:
ServerName does not take in wildcards, so you must define it with the main "default" virtualhost name matching the scheme you want to use, then you use ServerAlias with wildcards or several entries to match all those domain requests that have to land in the same virtualhost
Note the xyz-portal.example.com must be defined first. Why? Because the generic wildcard of the other virtualhost serveralias "*.example.com" would match and grab the request if defined first. Apache selects virtualhost to reply based on Host header requested and first match in the order of loaded virtualhost wins.

VirtualHost - point all domains to root folder and one domain to a subfolder in the root folder

I have tried but nothing seems to work for me here. I have an Apache server with multiple domains set up. Currently they all point to the root folder (C:\xampp\htdocs) but I'd like to organize things a little better here on. I'd like to use Apache's VirtualHost to point each new domain to a subfolder inside this folder but have all older websites point to C:\xampp\htdocs in the mean time until I can migrate each.
The issue is that when I use the following code, all my domains point to the subfolder
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\newdomain\"
ServerName www.newdomain.com
</VirtualHost>
I need to add a Virtual that listens to all other domains and points them to C:\xampp\htdocs
But I'm running out of ideas. Please help
Try that:
# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs"
ServerName _default_
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\newdomain"
ServerName www.newdomain.com
</VirtualHost>
You need a default virtualhost, that's the catch_all thing, when the Host Header defined in the HTTP query does not match any ServerName directive the default VH is useD. And the default VH is the first one defined in the configuration.

Apache Virtual Host using mod_vhost_alias

I am trying to set up my apache module to dynamically direct all requests to a specific folder and then match the name to a folder of the same name.
To do this I set the following in my 000-default.conf file in the sites-available folder.
UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2
This worked great.
Then I wanted to setup a couple of different domains to not point to the example folder, but somewhere else, so I added a couple of these before the VirtualDocumentRoot line:
<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>
However, now the dynamic pointing does not work anymore and all the URL's are redirected to the first -> VirtualDocumentRoot location.
Can someone please indicate to me what I am doing wrong?
Full Code Example In apache2/sites-available/000-default.conf:
<VirtualHost *:80>
ServerName sub1.example.com
VirtualDocumentRoot /var/www/sub1.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub2.example.com
VirtualDocumentRoot /var/www/sub2.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName sub3.example.com
VirtualDocumentRoot /var/www/sub3.example.com
</VirtualHost>
UseCanonicalName Off
VirtualDocumentRoot /var/www/example/%2
Do not use VirtualDocumentRoot for simple Virtualhosts, use only DocumentRoot.
VirtualDocumentRoot defines the mass-virtualhost catch-all, and by definition you can only have one mass-virtualhost (else how could apache knows which VH a given hostname should match).
Edit:
Now you need some other changes:
- ensure you have NameVirtualHost *:80 somewhere in apache configuration (unless you use Apache 2.4).
- Move the Mass-Virtualhost as first, so it will become the default virtualhost. The default virtualhost is used when the request host name does not match any ServerName directive. (You can check the default VH by running apache with -S option).
I have figured out how to do this, and decided to post the solution here for anyone else sitting with a similar problem:
SO to setup apache2, using mod_vhost_alias to have all domains point to a generic folder with the same name, but specific domains to point elsewhere, this is what you need to do.
In your 000-default.conf site config file, write the following code:
UseCanonicalName Off
Then add the following block for each specific domain you want to point to a specific folder, replacing example.com with your domain name:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.*
DocumentRoot path/to/your/folder
</VirtualHost>
Then add the next block to point all other generic domains to a generic folder:
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias www.*
VirtualDocumentRoot path/to/your/folder/%2+
</VirtualHost>
<VirtualHost *:80>
ServerName vhosts.fqdn
ServerAlias *
VirtualDocumentRoot path/to/your/folder/%1+
</VirtualHost>
The first block will direct all domains, starting with www. to a folder matching the name after the www.
The second block is to direct the same domains, when no www. is specified, to the same folder.
For more information on the dynamic mass virtual host options to use in the document root, go to: http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html

Htaccess non sensible subdomains

While I was searching for a hosting I found one called co.gp (they have more), so this hosting was great and I discovered something on it that it allows you to put any fakes subdomain you want for example.
If your website it's example.org the hosting allows you to put test1.example.org or test2.example.org.
I searched alot with the help of htaccess but I couldn't make one Like I'm looking for. So my question here. How Can I make fakes subdomains in my website ?
Ps : I don't want subdomains to be redirect for example if someone type test.example.org/something I want it to be the same as example.org/something.
Htaccess files will not be of much help here, because they are located under the document root, and are therefore evaluated after apache has determined which virtualhost points to this specific document root.
Assuming your DNS entries are well-configured, You should instead modify directly the apache main configuration.
If every subdomain points to a different document root, you will have to create a virtual host for each one:
<VirtualHost *>
ServerName example.org
DocumentRoot /path/to/example.org
# ...
</VirtualHost>
<VirtualHost *>
ServerName test1.example.org
DocumentRoot /path/to/test1.example.org
# ...
</VirtualHost>
If, instead, several subdomains point to a single document root, you can use the ServerAlias directive:
<VirtualHost *>
ServerName example.org
ServerAlias test1.example.org
DocumentRoot /path/to/example.org
# ...
</VirtualHost>
If all of the possible subdomains point to the same document root, you can relieve yourself from the pain of listing every subdomain in the ServerAlias list, and use a wildcard instead:
<VirtualHost *>
ServerName example.org
ServerAlias *.example.org
DocumentRoot /path/to/example.org
# ...
</VirtualHost>
There are of course more configurations directives needed to ensure each virtual host works properly, but these are the main building blocks you should understand to have a working setup.