ordering VirtualHosts in Apache conf - apache

I'm having issues with my apache configuration. The expected outcome is to allow all traffic to go to the default site trekfederation.com, UNLESS the subdomain is specified and matched otherwise. My problem is that some of my subdomains still default to the main site.
I have two files as part of my configuration:
httpd.conf
ships.conf
here's httpd.conf's portion for virtualhost:
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
# ServerAdmin webmaster#dummy-host.example.com
# DocumentRoot /www/docs/dummy-host.example.com
# ServerName dummy-host.example.com
# ErrorLog logs/dummy-host.example.com-error_log
# CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>
<VirtualHost *:80>
ServerName weather.trekfederation.com
ServerAlias weather.trekfederation.com
DocumentRoot "/var/www/html/weather"
</VirtualHost>
<VirtualHost *:80>
ServerName oldsite.trekfederation.com
ServerAlias oldstore.trekfederation.com
DocumentRoot "/var/www/html/trekfed"
</VirtualHost>
<VirtualHost *:80>
ServerName promenade.trekfederation.com
Redirect / http://trekfederation.com/shop
</VirtualHost>
<VirtualHost *:80>
ServerName donations.trekfederation.com
Redirect / http://trekfederation.com/product-category/donations/
</VirtualHost>
Include conf.d/ships.conf
<VirtualHost *:80>
ServerName trekfederation.com
ServerAlias trekfederation.com www.trekfederation.com
Alias /phpmyadmin "/usr/share/phpMyAdmin"
<Directory "/usr/share/phpMyAdmin">
DirectoryIndex index.php
AllowOverride All
Options FollowSymlinks
Order allow,deny
Allow from all
</Directory>
ServerAlias www.iftcommand.com
DocumentRoot "/var/www/html/trekweb"
</VirtualHost>
# Include conf.d/personal.conf
<VirtualHost *:80>
ServerName ccc.trekfederation.com
DocumentRoot "/var/www/html/ccc"
</VirtualHost>
Ships.conf:
<VirtualHost *:80>
ServerName ussgeorgewashington.trekfederation.com
ServerAlias ussgeorgewashington.trekfederation.net
ServerAlias nfc1773a.trekfederation.com
ServerAlias nfc1773a.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_GEORGE_WASHINGTON
</VirtualHost>
<VirtualHost *:80>
ServerName ussfarragut.trekfederation.com
ServerAlias ussfarragut.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_FARRAGUT
</VirtualHost>
<VirtualHost *:80>
ServerName ussvortex.trekfederation.com
ServerAlias ussvortex.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_VORTEX
</VirtualHost>
<VirtualHost *:80>
ServerName ussvictory.trekfederation.com
ServerAlias ussvictory.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_VICTORY
</VirtualHost>
<VirtualHost *:80>
ServerName ussnavigator.trekfederation.com
ServerAlias ussnavigator.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_NAVIGATOR
</VirtualHost>
<VirtualHost *:80>
ServerName ussfirelace.trekfederation.com
ServerAlias ussfirelace.trekfederation.net
DocumentRoot /var/www/html/NOVA/USS_FIRELACE
</VirtualHost>
the site i'm working on is ussfirelace.trekfederation.com. it still routes to trekfederation.com.
it does register with apache -S:
port 80 namevhost ussfirelace.trekfederation.com (/etc/httpd/conf.d/ships.conf:34)
alias ussfirelace.trekfederation.net
what am I missing?
Thanks.

The configs look good to me. Possible solutions / problems:
a) Did you restart your apache to load the new virtual host?
b) Have you checked that the DNS record points as A Record (or CNAME) to your server and no redirect is configured with your NS provider?
c) Have you checked your code, that it does not trigger a redirect (e.g. 301, or javascript...)?
d) Clearing browser cache is also some thing that you should take care of.
e) Doublecheck the URL you type for typos.

Related

how to redirect browser to two different site with apache virtualHost?

I have a valid IP, e.g. x.x.x.x, and a domain, e.g. site.com which point to x.x.x.x.
First, I need to redirect any request from port 80 to 443 (security issue).
Second, for every request sent to my IP, I want apache to show "Hello it's
working" (/var/www/index.html) and everyone request to my domain to show the real site.
I tried this, but it did not work:
<VirtualHost x.x.x.x:433>
ServerName x.x.x.x
ServerAlias x.x.x.x
DocumentRoot /var/www/
DirectoryIndex index.html
Options -Indexes
</VirtualHost>
<VirtualHost site.com:443>
...
</VirtualHost>
for redirection:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.yourdomain.com
Redirect / https://www.yourdomain.com
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.yourdomain.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
for domain separation:
<VirtualHost *:80>
# This first-listed virtual host is also the default for *:80
ServerName www.example.com
ServerAlias example.com
DocumentRoot "/www/domain"
</VirtualHost>
<VirtualHost *:80>
ServerName other.example.com
DocumentRoot "/www/otherdomain"
</VirtualHost>

Apache virtual hosts not working when using subdomain

I am trying to setup a virtual host. I have done this many times before on different servers with no problem, however, I have never tried it using a subdomain.
For some strange reason when I go to the subdomain: sub.mysite.co.uk I get redirected to the /var/www/mysite directory and not /var/www/other-site
Here is my apache.conf file:
Include /etc/apache2/sites-enabled/
NameVirtualHost *:80
<ifmodule mod_ssl.c>
NameVirtualHost *:443
</ifmodule>
<VirtualHost *:80>
ServerName *.mysite.co.uk
DocumentRoot "/var/www/newsletters/"
</VirtualHost>
And my sites-enabled file:
<VirtualHost *:80>
ServerName mysite.co.uk
DocumentRoot /var/www/newsletters
ServerAlias sub.mysite.co.uk
</VirtualHost>
Have I missed something?
Did you check your DNS entries? Maybe it's redirecting all subdomains to mysite.co.uk.
Write this in httpd.conf
Include /etc/apache2/sites-enabled/*.conf
Listen *:80
Write this in custom-vhost.conf located in sites-enabled
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin contact#example.com
DocumentRoot /var/www/domain1
ServerName domain1.me
ServerAlias www.domain1.me
</VirtualHost>
<VirtualHost *:80>
ServerAdmin contact#example.com
DocumentRoot /var/www/domain2
ServerName domain2.com
ServerAlias www.domain2.com
</VirtualHost>
Hope it works

defining VirtualHosts in apache fails

i tried using the vhost file (httpd-vhosts.conf) in apache to define different directories for different domain names. i defined it as follow and restarted apache.
no success - when i try to reach www.domain.mx it does not take me to the path mentioned in the documentroot.
i made sure the vhost file is included in the httpd.conf file and its module is loaded.
what am i doing wrong?
NameVirtualHost 12.12.65.90:80
NameVirtualHost domain.mx:80
NameVirtualHost www.domain.mx:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost 12.12.65.90:80>
ServerAdmin webmaster#23.21.65.90
DocumentRoot "C:/xampp/htdocs/"
ServerName 12.12.65.90
ServerAlias http://12.12.65.90/
ErrorLog "logs/general-errors.log"
CustomLog "logs/general.log" combined
</VirtualHost>
<VirtualHost domain.mx:80>
ServerAdmin webmaster#domain.mx
DocumentRoot "/taska/"
ServerName domain.mx
ServerAlias domain.mx
ErrorLog "logs/domain-errors.log"
CustomLog "logs/domain.log" combined
</VirtualHost>
<VirtualHost www.domain.mx:80>
ServerAdmin webmaster#domain.mx
DocumentRoot "/taska/"
ServerName www.domain.mx
ServerAlias www.domain.mx
ErrorLog "logs/domain-errors.log"
CustomLog "logs/domain.log" combined
</VirtualHost>
apparently this is the way to do it (using serverAlias):
NameVirtualHost *:80
#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
ServerAdmin webmaster#domain.mx
DocumentRoot "c:/xampp/htdocs/taska"
DirectoryIndex taska.html
ServerName domain.mx
ServerAlias domain.mx
ErrorLog "logs/domain-errors.log"
CustomLog "logs/domain.log" combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#domain.com
DocumentRoot "c:/xampp/htdocs/taska"
DirectoryIndex taska.html
ServerName www.domain.mx
ServerAlias *.domain.mx
ErrorLog "logs/domain-errors.log"
CustomLog "logs/domain.log" combined
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#21.11.65.90
DocumentRoot "c:/xampp/htdocs/"
ServerName localhost
ServerAlias 21.11.65.90
ErrorLog "logs/general-errors.log"
CustomLog "logs/general.log" combined
</VirtualHost>
<Directory C:/xampp/htdocs/taska>
Order Deny,Allow
Allow from all
</Directory>

Different VirtualHosts with the same port

I need to have two VirtualHosts with the same listen port for different projects and with different logs. Here's what I've got:
<VirtualHost *:80>
DocumentRoot /home/projects/smk
ErrorLog /var/log/apache2/smk-error.log
RedirectMatch ^/$ /cms
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /home/projects/smk/cms
ErrorLog /var/log/apache2/smk-cms-error.log
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /home/projects/smk/deploy
ErrorLog /var/log/apache2/smk-deploy-error.log
</VirtualHost>
Add different ServerName directive in all virtual hosts:
<VirtualHost *:80>
ServerName dev.localhost
DocumentRoot /home/projects/smk/cms
ErrorLog /var/log/apache2/smk-cms-error.log
</VirtualHost>
<VirtualHost *:80>
ServerName my-project.localhost
DocumentRoot /home/projects/smk/deploy
ErrorLog /var/log/apache2/smk-deploy-error.log
</VirtualHost>
Don't forget to add host-entries for dev.localhost and my-project.localhost in /etc/hosts to 127.0.0.1 or whatever ip you want it to point to.
ServerName my-project.localhost
DocumentRoot /home/projects/smk/deploy
ErrorLog /var/log/apache2/smk-deploy-error.log
//Try adding Error document
ErrorDocument 404 404.html
There is a need to create a feedback in your application.

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>