Use Apache server alias to use the same Matomo installation for different web sites

Say I have two web sites hosted on the same Apache server:

I could install Matomo on the server and use stats.example.org for both web sites.

  • example.org tracking code points to: stats.example.org
  • example.com tracking code also points to: stats.example.org

But what if I want to use the .com domain for tracking, like this:

  • example.org tracking code points to: stats.example.org
  • example.com tracking code points to: stats.example.com

Is it possible on the Apache server to make an alias so that requests to stats.example.com actually get redirected to stats.example.org on the server?

It is possible. Point the subdomain stats.example.com not to the matomo directory, but to a symlink named “matomosymlink” there is linked to the matomo directory. Than you can use stats.example.com and it used the same matomo installation.

An another solution is possible with DNS CNAME (cloaking).

Thanks for a fast answer! Even though it takes more steps (maybe even making a SSL certificate for the stats.example.com subdomain?) your suggested symlink solution sounds like it could work. Have you tried it yourself?

Setting up DNS CNAME is probably a little bit too complicated, for most users …

After sleeping on it and experimenting a bit, rather than making a new stats.example.com sub-domain, it’s simpler to use Apache’s Alias Directive to map a URL to a filesystem location:

The Alias directive allows documents to be stored in the local filesystem other than under the DocumentRoot. URLs with a (%-decoded) path beginning with URL-path will be mapped to local files beginning with directory-path.

These steps did it:

  1. Define an alias in /etc/apache2/mods-available/alias.conf pointing to stats.example.org:
    Alias "/stats" "/var/www/html/stats.example.org/public_html"
  2. Add trusted_hosts[] = "example.com" in config/config.ini.php to avoid “Warning: You are now accessing Matomo from …” message
  3. Point example.com Matomo script to https://example.com/stats

NOTE:

  • Add the alias in /etc/apache2/mods-available/alias.conf, not in the virtual host file, which I tried, but couldn’t make work
  • This will add an alias for all domains, which is fine for me. But perhaps someone knows how to limit the aliasing by folder?

Yes, all sub/domains must have a SSL cert, when https is in use. Than it is very simple.

In your Apache’s Alias Directive method, the user must have an access to the apache configuration. The symlink methode is available with typical webhost accounts without apache access.

Works the Apache’s Alias Directive method without a SSL cert for the aliased subdomain?

example.com has SSL … or are you asking something else?

This is not what you want above, but its maybe a solution.

Maybe, with the symlink method a SSL cert for each sub/domains is not necessary.

First, we need more infos.

SSL cert exist?

Serverside pointing:

Yes, my plan was originally to have a subdomain stats.example.com, but I realized that I then needed to create an entire subdomain, generate SSL with lets encrypt, for it, etc. Way too much work.

This is what I meant with “After sleeping on it and experimenting a bit, rather than making a new stats.example.com sub-domain …”. Sorry if it wasn’t clear enough.

So that’s why I decided to use example.com/stats instead, and Apache Alias. It seems to be working well.

also a solution without subdomains. By this, the SSL cert status is not needed.

  • matomo installation in /var/www/html/matomo

  • example.com/var/www/html/example.com

  • example.org/var/www/html/example.org

symlinks:

  • /var/www/html/example.com/stats/var/www/html/matomo
  • /var/www/html/example.org/stats/var/www/html/matomo

You understand this? Its an example.

Also, the matomo is in /var/www/html/matomo.
The domains points to shes web public folder.
Inside this folders is a symlink named stats.
Both stats symlinks points to the folder with the matomo installation.

Yet you can use https://example.com/stats/ AND https://example.org/stats/ to link to the matomo backend or matomo.js.

no guaranty, no warranty.

Thanks for sharing an alternative solution using symlinks, not requiring SSL.

I did at first consider that solution, but had to drop it, since many CMS’es remove all files and folders during an update.

The benefit of using Apache Alias is that the configuration is persistent. But for more hand-held, or simpler web sites, a symlink in the root could work.

In any case, I feel like there should be a FAQ page (such as How do I configure my server to let Matomo serve HTML files?) about this use case, outlining how to use Matomo with Apache Alias or symlink, using the info we shared here. Something like this?

How do I use a single Matomo installation for different web sites, using the individual domain?

Say you have two (or more) web sites, and want to share a single Matomo installation, with this structure:

  • Matomo installation in /var/www/html/matomo
  • example.com in /var/www/html/example.com
  • example.org in /var/www/html/example.org

It is possible for both https://example.com/stats AND https://example.org/stats to access that single Matomo installation, either using symlinks or Apache Alias to map a URL to a filesystem location.

Since many CMS’es remove all files and folders during an update, the benefit of using Apache Alias is that the configuration is persistent. But for more hand-held, or simpler web sites, a symlink in the root could work.

Using Apache alias

Note: Add the alias in /etc/apache2/mods-available/alias.conf, not in the virtual host file, which doesn’t work. This will add an alias for all domains, so you might want to limit the aliasing by folder.

  1. Define an alias in /etc/apache2/mods-available/alias.conf pointing to the Matomo installation folder, and restart Apache:
    Alias "/stats" "/var/www/html/matomo"

Using symlink

  1. Create two symlinks pointing /stats in the public web site folder to the Matomo installation folder with these commands:
    ln -s /var/www/html/example.com/stats /var/www/html/matomo
    ln -s /var/www/html/example.org/stats /var/www/html/matomo

Final configuration for both Apache Alias and symlink

  1. Add the two web sites in config/config.ini.php to avoid “Warning: You are now accessing Matomo …” message:
    trusted_hosts[] = "example.com"
    trusted_hosts[] = "example.org"
  2. In both web sites, point the Matomo tracking URL to example.com/stats and example.org/stats

@ra , its possible. Please wait a week (waiting for other comments here) and than open an new issue as “c: Documentation” in Github.
The label “c: Documentation” is for the matomo documentation pages.
And push a solution with CNAME in your tutorial.

Thanks for the link @melbao, I might do that, or Matomo support should feel free to add it themselves.

I don’t know how to set up CNAME for this, so perhaps you can write a short text about the steps required and post it here?

A CNAME-Record (canonical-name-record) in the DNS (domain name system) is like a symlink in the filename system (filesystem).

The CNAME-Record is a domain or a subdomain to which a domain or subdomain points. But its not a Redirect and not a RewriteRule. Its like symlink. The content of an cname-domain is (also) delivered with a cnamed domain.

DNS
Domain: example.com
Name - Record - Value
www - CNAME - www.example.org

The domain www.example.com will display the content from www.example.org, but in the browser address bar is displayed and the referer is always www.example.com.

With HTTPS (SSL) it is necessary, that both domains have a cert.

The term “canonical” is known from HTML link attribute in the head.
<link rel="canonical" href="http://example.com/">
With this, the searchbots known, that the original content is published under example.com. With CNAME is this not needed.

CNAME is further named “cloaking” and has a negative image in the internet management.

By the way: Here in Matomo is all DIY. Either you do it, or no one does. :wink: I am only a matomo user, not a matomo developer.

Thanks for the write up @melbao!

Though, as you write, using CNAME is also referred to as cloaking, and can be seen as something nefarious … especially among the privacy oriented.

And since many use Matomo exactly due to privacy concerns, I am not sure it would be a great idea to include it in the FAQ page, should it ever get created … just my 2 cents.

And about getting things done, and this page included in the FAQ documentation: It would seem to me a loss for Matomo, if the developers don’t follow along here in the forum, and if they see something like this issue, adapt it for the documentation, since us users seem to have a need for it.

Anyway, you and me have now created a resource, available for anyone on the internet, who can now find it, if they search for https://presearch.com/search?q=matomo+alias+apache :slight_smile: