Apache VirtualHost and Wildcard SSL - apache

I am trying to configure my website so that :
www.example.com alias example.com is served by a VirtualHost
*.example.com are serverd by another one with different DocumentRoot
My concern is that the first item works well, but if I call site1.example.com, it still shows /home/default_domain content, not /home/blogs/site1
Here is the configuration:
Listen 443 https
<VirtualHost *:443>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /home/default_domain
[SSL configuration]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [R=301,L]
</VirtualHost>
<VirtualHost *:443>
ServerName blabla.example.com
ServerAlias *.example.com
VirtualDocumentRoot /home/blogs/%1
[SSL configuration]
<Directory /home/blogs/>
Options MultiViews FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Related

Redirect ip address to https server name

So this is my conf:
<VirtualHost *:80>
<Directory /var/www/html/webserver/public>
Allow From All
AllowOverride All
Options -Indexes
</Directory>
DocumentRoot /var/www/html/webserver/public
ServerName ./install.sh
</VirtualHost>
<VirtualHost *:80>
<Directory /var/www/html/webserver/public>
Allow From All
AllowOverride All
Options -Indexes
</Directory>
DocumentRoot /var/www/html/webserver/public
ServerName test.com
ServerAlias www.test.com
Redirect permanent / https://test.com/
RewriteEngine on
#RewriteBase /
RewriteCond %{SERVER_NAME} =www.test.com [OR]
RewriteCond %{SERVER_NAME} =test.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
<Directory /var/www/html/webserver/public>
Allow From All
AllowOverride All
Options -Indexes
</Directory>
DocumentRoot /var/www/html/webserver/public
ServerName test.com
ServerAlias www.test.com
SSLCertificateFile /etc/letsencrypt/live/test.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/test.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
This works perfectly for http -> https redirection. For example test.com or http://test.com redirects to https://test.com. www.test.com does not work (I don't know why?), I'll get a 404 page. Next big thing, lets assume the ip of my webserver is 12.23.45.67 - how do I have to change my config file, so this ip also redirects to https://test.com?
new .conf file based on #Don't Panic post:
<VirtualHost *:80>
<Directory /var/www/html/webserver/public>
Allow From All
AllowOverride All
Options -Indexes
</Directory>
DocumentRoot /var/www/html/webserver/public
ServerName test.com
ServerAlias www.test.com
RewriteEngine on
#RewriteBase /
RewriteCond %{SERVER_NAME} =www.test.com [OR]
RewriteCond %{SERVER_NAME} =test.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ServerName XX.XX.XX.XX
ServerAlias www.test.com
DocumentRoot /var/www/html/webserver/public
RewriteEngine on
RewriteCond %{SERVER_NAME} =test.com [OR]
RewriteCond %{SERVER_NAME} =www.test.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
So my final solution looks like this:
<VirtualHost *:80>
<Directory /var/www/html/webserver/public>
Allow From All
AllowOverride All
Options -Indexes
</Directory>
DocumentRoot /var/www/html/webserver/public
Redirect / https://test.com
</VirtualHost>
This is working for nearly every redirection, but it's not working for www.test.com - Is this a good solution? May not. But it's kind of a working one.

Apache virtualhost root, www and sub-domain setup including http to https

I'm pretty stuck. Would appreciate some help.
Two websites (wordpress and another php tool) on an AWS EC2 Ubuntu
18.04 instance running LAMP stack.
My intention is to achieve:
http://www.example.com
http://example.com
https://www.example.com
to: https://example.com.
AND
http://app.example.com
to: https://app.example.com.
I had virtualhost configured and working as I intended, I then created the https cert through letsencrypt which setup the HTTP to HTTPS redirects...however...something's gone wrong:
HTTP all redirect to HTTPS.
https://app.example.com resolves to the intended website - website1.
https://example.com doesn't resolve to website2 - it resolves to website1 - note it's not redirecting to app. but showing website1 on https://example.com.
I've two virtualhost conf files, one for each site. Both contain each sites 80 and 443 config, they're below.
The SSL cert has the common name of example.com and lists alternative names for app.example.com, www.example.com and example.com.
DNS has example.com A to server IP, www. and app. are CNAME to example.com.
app.example.com.conf - Website 1
<VirtualHost *:80>
ServerAdmin jimmy#example.com
DocumentRoot /var/www/website1/
ServerName example.com
ServerAlias app.example.com
<Directory /var/www/website1/>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =app.example.com [OR]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin jimmy#example.com
DocumentRoot /var/www/website1
ServerName example.com
ServerAlias app.example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website1/>
Options FollowSymlinks
AllowOverride All
Require all granted
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
example.com.conf - Website 2
<VirtualHost *:80>
ServerAdmin jim#example.com
ServerName example.com
ServerAlias example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<VirtualHost *:80>
ServerAdmin jim#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin jim#example.com
ServerName example.com
ServerAlias example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin jim#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/website2
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<Directory /var/www/website2/>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
</IfModule>
I'd really appreciate any help, suggestions and any other ideas!
https://example.com doesn't resolve to website2 - it resolves to website1 - note it's not redirecting to app. but showing website1 on https://example.com
Yes, because all your vHosts define example.com as the ServerName and I expect website1 appears first in the config.
For app.example.com.conf (Website 1) you should set ServerName app.example.com and remove the ServerAlias directive altogether for both the vhost:80 and vhost:443 containers.
The HTTP to HTTPS redirect in the vhost:80 container:
RewriteEngine on
RewriteCond %{SERVER_NAME} =app.example.com [OR]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Can be simplified to a single mod_alias Redirect directive:
Redirect 301 / https://app.example.com/
For example.com.conf (Website 2) you have duplicated the vhost:80 and vhost:443 containers and defined the same ServerName example.com in each - this is an error. It is perfectly possible to have 4 containers like this (and use mod_alias Redirect directives), but you would need unique ServerName directives for each vHost and cut down the repetition.
The easiest approach from your point of view is probably just to remove the second (duplicate) vhost:80 and vhost:443 containers and set the following in both the vhost:80 and vhost:443 containers that remain.
ServerName example.com
ServerAlias www.example.com
I then created the https cert through letsencrypt which setup the HTTP to HTTPS redirects
Creating the SSL cert using LetsEncrypt should not have "setup the HTTP to HTTPS redirects"?

Server not reading htaccess file and redirect not working

My site is not reading the htaccess file. Even if I add Deny from all at the top, the site loads fine.
Permission is 644. I have a number of sites running on the server so Apache is set up correctly.
This is my virtualhost config:
<VirtualHost 12.34.56.78:80>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
<Directory /srv/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
This is what I'd added to .htaccess file to redirect www to non-www:
RewriteCond %{HTTP_HOST} ^(www\.)(.*) [NC]
RewriteRule (.*) https://%2%{REQUEST_URI} [L,R=301]
I tried adding it to virtualhost as well but it's still not redirecting.
After issuing an SSL Certificate with Let's Encrypt I had noticed it had created a duplicate conf file for port 443:
/etc/apache2/sites-available/example.com.conf
/etc/apache2/sites-available/example.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost 12.34.56.78:443>
ServerAdmin webmaster#example.com
ServerName example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example.com/public_html/
<Directory /srv/www/example.com/public_html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /srv/www/example.com/logs/error.log
CustomLog /srv/www/example.com/logs/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
Prior to the LE, the original example.com.conf didn't have the AllowOverride All directive. So I assume what has happened is the contents were duplicated and then I only changed the original one.

Redirect subdomains to different paths while using separate SSL certificates

I have a website with several different subdomains, and want to have parts of the site with higher levels of security than others. Such as Admin areas. I know that you can create as many certificates as you want. The issue that I am having is that whatever subdomain is listed first, it's DocumentRoot is applied to all the other subdomains redirecting to https://. Here is my code in httpd-vhosts.conf:
# HTTP Configuration
<VirtualHost *:80>
ServerName account.example.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DocumentRoot "/Applications/MAMP/htdocs/Website/Account"
</VirtualHost>
# SSL Configuration
<VirtualHost *:443>
ServerName account.example.com
SSLEngine on
SSLCertificateFile /Applications/MAMP/conf/apache/account.crt
SSLCertificateKeyFile /Applications/MAMP/conf/apache/account.key
DocumentRoot "/Applications/MAMP/htdocs/Website/Account"
</VirtualHost>
#Secure Admin Config
<VirtualHost *:80>
ServerName secure.example.com
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
# Other directives here
</VirtualHost>
#SSL Config for Admin area
# SSL Configuration
<VirtualHost *:443>
ServerName secure.example.com
SSLEngine on
SSLCertificateFile /Applications/MAMP/conf/apache/secure.crt
SSLCertificateKeyFile /Applications/MAMP/conf/apache/secure.key
DocumentRoot "/Applications/MAMP/htdocs/Website/Secure"
</VirtualHost>
For example, if I were to go to secure.example.com it's DocumentRoot would be /Applications/MAMP/htdocs/Website/Account instead of /Applications/MAMP/htdocs/Website/Secure
Is there a way to fix this?
Thank you in advance!

Apache: virtualhost on each sub-domain to corresponding directory

I would like to do something like this:
<VirtualHost *:80>
ServerName $variable.example.com
DocumentRoot /var/www/$variable
</VirtualHost>
for example if I go to foo.example.com it must show me /var/www/foo directory content
What is the good apache syntax for that?
You can't have $variable like that in servername and map it to the document root, but you can use mod_rewrite and a wildcard.
<VirtualHost *:80>
ServerName subdomain.example.com
ServerAlias *.example.com
DocumentRoot /var/www/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteCond %{REQUEST_URI}::%1 !^/([^/]+).*::\1
RewriteRule ^(.*)$ /%1/$1 [L]
</VirtualHost>
The subdomain.example.com can just be any subdomain that isn't www.example.com. For any request that doesn't start with "www", the subdomain is captured as a group (2nd condition), then the next line makes sure the request isn't already being routed into the subdomain's name, and the rule routes it.
So for the request:
http://foo.example.com/bar/x.html
it gets routed to:
/foo/bar/x.html
This is what I use with my freelancer work:
Under httpd-vhosts.conf:
UseCanonicalName Off
<VirtualHost *:${AP_PORT}>
DocumentRoot ${US_ROOTF_WWW}/_client
ServerName client
ServerAlias client
<Directory "${HOME}\www\_client">
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:${AP_PORT}>
VirtualDocumentRoot "${US_ROOTF_WWW}/_client/%1"
ServerName subdomains.client
ServerAlias *.client
<Directory "${HOME}\www\_client\*">
Require all granted
</Directory>
</VirtualHost>
Then under the windows hosts file place the domain and subdomains that are needed.
127.0.0.1 client
127.0.0.1 someone.client
127.0.0.1 someone2.client