apache 2.4 serverAlias with * wildcard overwrittes specificed vhost - apache

I have two vhosts the default one and another one:
<VirtualHost *:80>
ServerName www.eve-stuff.com
ServerAlias *.eve-stuff.com
DocumentRoot /var/www/html/test
<Directory /var/www/html/test>
Options +FollowSymLinks
AllowOverride None
#Require all denied
</Directory>
</VirtualHost>
This one should just redirect all subdomains that a not specifically setup to the test directory.
This is another vhost i have:
<VirtualHost *:80>
ServerName dev.eve-stuff.com
DocumentRoot /var/www/html/dev
<Directory /var/www/html/dev>
Options +FollowSymLinks
AllowOverride None
</Directory>
</VirtualHost>
This one should show the dev folder for the dev subdomain.
From what I understood from the apache doc the ServerAlias *.eve-stuff.com in the first host will always catch UNLESS another vhost has specifically been created for the address.
However dev.eve-stuff.com still redirects to the test folder not to the dev folder, as it should.
All dns entries, also for the subdomains, point to the right IP.
I am running Apache/2.4.18 (Ubuntu).

Turns out the order in which it is setup matters.
Apache apparently looks for the matching host it can find.
Since the first config was in 000-default.conf and the second one was in the 020-dev.conf the dev-subdomain matched the first vhost and used that.
I changed default to 999-default.conf which means now dev will first be matched with the proper vhost but other undefined subdomains will still be matched with the default vhost.

Related

Apache VirtualHost configure

After I configured virtual host, my apache document root changed to the virtual host's document root, I just want to know why.
here is my httpd-vhosts.conf:
<VirtualHost *:80>
ServerName myapp.zend
DocumentRoot /opt/lampp/htdocs/php_zend_projects/myapp
<Directory /opt/lampp/htdocs/php_zend_projects/myapp/public>
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_authz_core.c>
Require all granted
</IfModule>
</Directory>
</VirtualHost>
After I restart apache server, localhost page changed to index of /opt/lampp/htdocs/php_zend_projects/myapp,
http://gwjyhs.com/t6/702/1556725814x2728329017.png
but it is supposed to be xampp's default page like this:
http://gwjyhs.com/t6/702/1556726269x2728278877.png
If you enable vhosts you have to add an entry that looks like:
<VirtualHost *:80>
DocumentRoot "F:/Dev/xampp/htdocs"
ServerName localhost
</VirtualHost>
Note: change path to whatever is appropriate for you.
Restart webserver and it should work as before.
Reasoning behind this can be found in a comment on top of the httpd-vhosts.conf:
The first VirtualHost section is used for all requests that do not match a ##ServerName or ##ServerAlias in any block.
That means when you type in localhost it fallbacks to your myapp.zend vhost because it is (probably) the first virtualhost section.

Configure Apache to run website off of port-enabled IP address

To be perfectly honest, I'm not even sure if this is doable...
I've configured my vhosts file in /etc/apache2/sites-enabled which you can see here:
<VirtualHost 159.203.171.140:8080>
ServerAdmin webmaster#localhost
ServerName 159.203.171.140:8080
DocumentRoot "/home/wiki/public_html"
DirectoryIndex index.php index.html
<Directory "/home/wiki/public_html">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/wiki_error.log
CustomLog ${APACHE_LOG_DIR}/wiki_access.log combined
</VirtualHost>
for a digitalocean droplet given at the IP listed in the above hosts file. This droplet has absolutely nothing on it except for the wiki user in /home/ plus the required php, mysql/mariadb, apache stuff.
What I want to be able to do is to go to 159.203.171.140:8080 and see my site without having to purchase a useless domain name.
I'd really appreciate some help with this one.
If you have only one website on the droplet, then you don't need to set up a virtual host. You can use the 000-default.conf, no need for a2ensite.
You do not need the ServerName, which won't work with the IP as a name, you also don't need the IP address in the VirtualHost directive.
So, instead of this:
<VirtualHost 159.203.171.140:8080>
ServerAdmin webmaster#localhost
ServerName 159.203.171.140:8080
DocumentRoot "/home/wiki/public_html"
...
You can use this in your 000-default.conf file
<VirtualHost *:8080>
DocumentRoot "/home/wiki/public_html"
...
The rest of the directive stays as you have it.
Also, one note, if you are using port 8080, then you need go to /etc/apache2/ports.conf and set the Listen to 8080 (restart Apache after doing this).

apache VirtualHost redirecting to different VirtualHost

I am very new to Apache2 and I am attempting to serve two websites on a server.
I have followed the documentation here: https://httpd.apache.org/docs/current/vhosts/examples.html
and have attempted:
<VirtualHost *:80>
DocumentRoot "var/www/websiteexample1.com"
ServerName www.websiteexample1.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "var/www/websiteexample2.com"
ServerName www.websiteexample2.com
</VirtualHost>
However if i attempt to go to www.websiteexample2.com I am instead directed to the content of www.websiteexample1.com
What am i missing?
Your issue may be that you have not setup an associated Directory in the main conf file? Something along the lines of :-
#======================================================================
# Note this one is for the secondary root (for www.websiteexample2.com
# This should be changed to whatever you set DocumentRoot to.
<Directory "Your 2nd Website's Directory here">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
You also need to tell Apache to use/look at the virtual hosts file. So if you haven't got the following, ensuring that it is not commented out and that it points to the correct location :-
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
This based upon a 2.2.6 version of Apache

Configure MAMP to treat each directory in Htdocs as Root in Apache mod_redirect

The issue is that when using MAMP for local development, the .htaccess file needs to differ in that the RewriteBase needs to specify the subdirectory for the specific site (as shown here on SO).
Is there a way to configure MAMP MAMP/conf/apachehttpd.conf so that each virtual site gets it's own "root"?
UPDATE
After getting a clue about Virtual Hosts:
Have updated /etc/hosts file to include:
127.0.0.1 ClientSite.localhost
Uncommented the line:
`#Include /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf`
in /Applications/MAMP/conf/apache/httpd.conf.
There is a directory called ClientSite in /Users/myname/Sites/.
This is the /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf content:
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
ServerName localhost
ServerAlias *.localhost
VirtualDocumentRoot /Users/myname/Sites/%0
RewriteLogLevel 3
RewriteLog "/Applications/MAMP/logs/rewrite.log"
<Directory /Users/myname/Sites>
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Restarted the MAMP servers (and even ran dscacheutil -flushcache).
When browser is pointed to ClientSite.localhost it returns a 404: The requested URL / was not found on this server.
so that each virtual site gets it's own "root"?
You need to use VirtualDocumentRoot.
This is how I am using this on my MAMP in my /Applications/MAMP/conf/apache/extra/httpd-vhosts.conf file:
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
ServerName localhost
ServerAlias *.localhost
VirtualDocumentRoot /Users/admin/htdocs/%0
RewriteLogLevel 3
RewriteLog "/Applications/MAMP/logs/rewrite.log"
<Directory /Users/admin/htdocs>
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Take note of VirtualDocumentRoot /Users/admin/htdocs/%0 directive. That makes each virtual site's root as:
VirtualDocumentRoot /Users/admin/htdocs/localhost
VirtualDocumentRoot /Users/admin/htdocs/dev.localhost
VirtualDocumentRoot /Users/admin/htdocs/client2.localhost
etc.
Then simply create a directory within /Users/admin/htdocs/ for each site named as above, like:
dev.localhost
client2.localhost
Remove (or rename) any .htaccess files during the process - and once websites confirmed to be accessible via url like: http://client2.localhost, .htaccess files should behave as expected.
Also be sure that in the /etc/hosts file, there's an entry like:
127.0.0.1 client2.localhost
for each URL in question.

redirect subdomains bar one

So I've set up a Amazon EC2 and registerd a few domains with 123-reg, i've set up my Apache VirtualHost But now I'm going to need to set up a few sub domains (like kitten.example.com). But it's only going to be a few, and I would like the rest of the subdomain wild cards to go to the base like this:
kitten.example.com -> kitten.example.com BUT
*.example.com -> example.com
currently my DNS with 123-reg is as follows:
www A 198.168.0.0
kitten A 198.168.0.0
* A 198.168.0.0
and my Apache httpd.conf is:
<VirtualHost *:80>
DocumentRoot "/var/www/example.com"
ServerName example.com
ServerAlias example.com
<Directory "/var/www/example.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/kitten.example.com"
ServerName kitten.example.com
ServerAlias kitten.example.com
<Directory "/var/www/kitten.example.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
so how do i redirect all those wildecard subdomains to my base domain?
You will have to map DocumentRoot and Directory to your base domain directory.
or if you wish to change the domain name also, then write a .htaccess rule for it.
Create a virtual host directive that doesn't specify the ServerName or ServerAlias. This will catch all virtual hosts on that IP address (you specifcy all IP's on port 80) that do not map to any other virtual host specification.
<VirtualHost *:80>
DocumentRoot /www/default
...
</VirtualHost>
http://httpd.apache.org/docs/2.2/vhosts/examples.html#default
However, if you want to send a 301 HTTP Header (permanently redirected), you'll need to use mod_rewrite or something similar:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
How to setup Apache mod_rewrite to redirect all but one subfolder is one good example .. needs to be modified to suit your needs if the first option isn't sufficient for your needs.