Virtualhost For Wildcard Subdomain and Static Subdomain - apache

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>

Related

apache virtualhost - localhost and development alias/servername

I'm trying to configure my apache vhosts file to have a localhost/something hostname and "alias" hostnames. I'm working with google api's currenctly and they are not accepting custom aliases as url's, so I can't make it work with my custom url's. Any thoughts of what to do? My current config that's not working:
<VirtualHost 127.0.0.1:80>
ServerName localhost/go
ServerAlias localhost/go
DocumentRoot "D:/username/Web/server.dev/go"
</VirtualHost>
<Directory "D:/username/Web/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Allow from all
Require all granted
</Directory>
<VirtualHost *:80>
ServerName api.server.dev
ServerAlias api.server.dev
DocumentRoot "D:/username/Web/server.dev/api"
</VirtualHost>
##... more custom urls with subdomains cut out because it's unnecessary
<VirtualHost *:80>
ServerName adstrck.server.dev
DocumentRoot "D:/username/Web/server.dev/adstrck"
</VirtualHost>
### ALL OTHERS ###
<VirtualHost *:80>
ServerName www.server.dev
ServerAlias server.dev *.server.dev
DocumentRoot D:/username/Web/server.dev
</VirtualHost>
When I'm trying to access 127.0.0.1/go or localhost/go I get an internal server error.
Maybe what you want is something like this
<VirtualHost 127.0.0.1:80>
ServerName localhost
ServerAlias server.dev *.server.dev
DocumentRoot "D:/username/Web/server.dev"
</VirtualHost>
<Directory "D:/username/Web/server.dev">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Allow from all
Require all granted
</Directory>
Then use a url like http://localhost/go to view the site.
Depending on your OS/browser, you may be able to add a development subdomain to localhost. E.g.
<VirtualHost *:80>
ServerName dev1.localhost
## rest of your config
## e.g. ServerAlias my.website.on.the.internet.com
DocumentRoot /var/www/dev1
</VirtualHost>
<VirtualHost *:80>
ServerName dev2.localhost
DocumentRoot /var/www/dev2
</VirtualHost>
# Default / catch-all
<VirtualHost *:80>
DocumentRoot /var/www/html
</VirtualHost>
I then pointed my browser to dev1.localhost and that resolved to dev1 and likewise for dev2.localhost and localhost by itself resolved to the default apache page.
This resolved my similar problem. Tested on Apache in a Debian WSL. Worked on Windows Chrome, failed on Windows Firefox. Based on this SO: https://stackoverflow.com/a/35124491

One-line configuration for both www and non-www VirtualHost

I have many VirtualHost and it seems that we need to do these two lines each time:
<VirtualHost *:80>
...
ServerName www.example.com
ServerAlias example.com
Is there a one-line solution that allows to do this?
<VirtualHost *:80>
ServerName *.example.com # works for both http://example.com and http://www.example.com
i.e. without having to duplicate example.com?
No, it is not possible.
ServerName is used to uniquely identify protocol, name and port of a host. It accepts only one parameter and no wildcard.
You can use wildcards in ServerAlias, and also specify multiple names on it:
ServerAlias *.example.com
or
ServerAlias www1.example.com www2.example.com www3 foo
Source: https://httpd.apache.org/docs/2.4/mod/core.html#serveralias
A trick I like to do is
<VirtualHost *:80>
Include /.../example.com.conf.include
</VirtualHost>
<VirtualHost *:443>
Include /.../example.com.conf.include
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf #optional, maybe you should not do that
</VirtualHost>
and the xxx.conf.include file like:
ServerAdmin someone#somewhere.tld
ServerName example.com
ServerAlias example.com *.example.com andmore.example.com
DocumentRoot /var/www/html/example.com
ErrorLog /.../example.com_error.log
CustomLog /.../example.com_custom.log combined
<Directory /var/www/html/example.com>
Options Indexes FollowSymLinks
AllowOverride AuthConfig
Order allow,deny
allow from all
</Directory>
This way, you can have HTTP and HTTPS virtual hosts in exactly the same configuration file.
Specifically to your question,
ServerAlias *.example.com
should do the trick

apache 2.2 virtualhost multiple domain ssl - google

Good Day,
I have multiple domains assigned to virtualhosta but only for one domain I have a certificate.
google downloads certified links and assigns them to domains without a certificate.
For example:
The certified domain has a subpage: https://mydomain1.pl/page/1.html - This works fine....
But it also creates links without the certificate https://mydomain2.pl/page/1.html, https://mydomain3.pl/page/1.html, https://mydomain4.pl/page/1.html - such page does not exist.
My virtualhost:
<VirtualHost *:80>
ServerName mydomain2.pl
ServerAlias mydomain2.pl
DocumentRoot /var/www/www/...
<Directory /var/www/www/.....>
...
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName mydomain3.pl
ServerAlias mydomain3.pl
DocumentRoot /var/www/www/...
<Directory /var/www/www/.....>
...
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName mydomain4.pl
ServerAlias mydomain4.pl
DocumentRoot /var/www/www/...
<Directory /var/www/www/.....>
...
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName mydomain1.pl
ServerAlias mydomain1.pl
Redirect / https://mydomain1.pl/
</VirtualHost>
<VirtualHost *:443>
ServerName mydomain1.pl
ServerAlias mydomain1.pl
DocumentRoot /var/www/www/..
<Directory /var/www/www/...>
SSLEngine on
.......
</VirtualHost>
How can I fix it ? For demos that do not have a certificate and have already been indexed by google redirect to the appropriate domain and page, or how to disable https for domains without https

ordering VirtualHosts in Apache conf

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.

How to make an redirectSSL in the vhost

I get slightly crazy :)
I have a domain example.com and I have a SSL certificate for www.example.com
The example.com refers to the IP address of the server (it is an EC2 instance).
In the vhost.conf of the Server I have the following entries
<VirtualHost *:80>
ServerName example.com
Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.example.com
DocumentRoot /var/www/vhosts/example-wp
SSLEngine On
...
</VirtualHost>
I took the information from the apache wiki.
https://wiki.apache.org/httpd/RedirectSSL
Thanks for helping
Tristan
You already have the necessary directives listed to perform the redirect. You also need to tell the vhost file where your certificate and key for the certificate exist. You also need to say whether or not a directory the user browses to is to load SSL. Please see a full example configuration file below.
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
Redirect / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
ServerName www.example.com
ServerAlias example.com
ServerAdmin webmaster#local
DocumentRoot /path/to/web/content
DirectoryIndex index.php index.html
ErrorLog logs/error_log
CustomLog logs/access combined
SSLEngine on
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLCertificateFile /etc/apache2/ssl.crt/certfile.cer
SSLCertificateKeyFile /etc/apache2/ssl.key/keyfile.key
<Directory "/path/to/web/content/">
Options None
AllowOverride None
Order allow,deny
Allow from all
SSLRequireSSL
</Directory>
</VirtualHost>