/~username AliasMatch

I recently purchased and installed an ssl security certificate on my private web server that hosts nearly 40 of my own web sites. Since more than one of those sites sells products, I need to use the ssl certificate on multiple sites. Rather than buy certificates for each one, I instead opted to process transactions on a secure subdomain of this site (secure.danifer.com) and alias the other web sites to subdirectories of the secure subdomain.

For example, instead of making https://howtoadvertise.net/shop I’ll use https://secure.danifer.com/~howtoadvertise.net/shop to accept encrypted data for credit card transactions. This way I won’t have to buy a separate security certificate for each site.

The problem

I couldn’t figure out how to configure the Apache2 virtualhost file to match both the fully qualified domain name as well as a sub-directory of the secure domain. I’ve worked with hosting reseller programs using the common Cpanel setup and I know it’s possible, I just didn’t know how to do it myself.

The solution

As it turns out, all I needed to do was make use of the Apache2 ServerAlias directive. Here’s the line that makes it work:

AliasMatch ^/~(.*) /home/public_html/$1

Which matches https://secure.danifer.com/~username to the public_html directory for any username on the system. Pretty slick!

Here’s my complete virtualhost file:

<VirtualHost *:80>
ServerName danifer.com
DocumentRoot /home/public_html/danifer.com
ServerAlias www.danifer.com
AliasMatch ^/~(.*) /home/public_html/$1
ErrorLog “|/usr/bin/cronolog /home/apache_logs/%Y-%m-%d-danifer.com-error.log”
CustomLog “|/usr/bin/cronolog /home/apache_logs/%Y-%m-%d-danifer.com-access.log” combined
CustomLog “|/usr/bin/cronolog /home/apache_logs/server_logs/%Y-%m-%d-server-access.log” combined

#Redirect non-existing requests to the homepage
ErrorDocument 404 /index.php
ErrorDocument 500 /index.php
ErrorDocument 403 /index.php
</VirtualHost>

<VirtualHost *:443>
ServerName secure.danifer.com
DocumentRoot /home/public_html/danifer.com
AliasMatch ^/~(.*) /home/public_html/$1
ErrorLog “|/usr/bin/cronolog /home/apache_logs/%Y-%m-%d-danifer.com-error.log”
CustomLog “|/usr/bin/cronolog /home/apache_logs/%Y-%m-%d-danifer.com-access.log” combined
CustomLog “|/usr/bin/cronolog /home/apache_logs/server_logs/%Y-%m-%d-server-access.log” combined

SSLEngine on
SSLCertificateFile /etc/apache2/ssl/ssl.crt
SSLCertificateKeyFile /etc/apache2/ssl/ssl.key
SSLCertificateChainFile /etc/apache2/ssl/ssl.crt

#Redirect non-existing requests to the homepage
ErrorDocument 404 /index.php
ErrorDocument 500 /index.php
ErrorDocument 403 /index.php
</VirtualHost>


Comments are closed.