Lesson 2.2: Matching by host name
A real multisite gives each brand its own host name: www.example.com for one site, www.example.org for the other.
The host probe chooses the siteaccess from the host the browser asks for. This lesson sets it up on the training
installation without touching DNS, and shows the host matching methods that let a test host such as
www.example.com.test.local reach the live site's siteaccess.
At a glance
| Course | Multisite and site packages |
| Module | Module 2: Matching requests to siteaccesses |
| Duration | 55 minutes (20 presenting, 35 exercise) |
| Level | Intermediate |
| Audience | Administrators and developers |
| Prerequisites | Lesson 2.1; curl on the learner's computer or the lab server |
Learning objectives
After this lesson, learners can:
- add the host probe to
MatchOrderand map a host to a siteaccess withHostMatchMapItems[]; - test host matching with
curland theHostheader before any DNS entry exists; - choose a host matching method (
strict,start,end,part) for the whole map or for one entry; - list what else a new host needs:
SiteURL, DNS, the web server or Velocity, the certificate.
Trainer's notes
How to present it
Sources: Siteaccess by host prefix, and the redirect to the language and Multisite on one installation.
The host map (5 minutes). Show the example:
# settings/override/site.ini.append.php [SiteAccessSettings] MatchOrder=host;uri HostMatchType=map HostMatchMapItems[] HostMatchMapItems[]=www.example.com;site HostMatchMapItems[]=www.example.org;site_org HostMatchMapItems[]=admin.example.com;adminHostMatchTypealso knowselement,textandregexp; the map is what multisites use.How hosts are compared (5 minutes). Every method compares host names as DNS does: without regard to case and to a trailing dot, without the port of the request unless the entry names one, and a name written in Unicode as the ASCII form a browser sends (when PHP has the intl extension).
The methods (5 minutes):
Method Matches when the host ... Example for www.example.comstrictis the listed host (default) www.example.comstartbegins with it www.example.com.test.localendends with it preview.www.example.compartcontains it a.www.example.com.bHostMatchMethodsets it for the whole map; a third field sets it for one entry (HostMatchMapItems[]=admin.example.com;admin;start).startandpartalso take hosts you did not mean, so use them only where the web server answers for your own hosts. The static cache only knowsstrict.Beyond the setting (5 minutes). Each siteaccess sets its own
[SiteSettings] SiteURL. The host needs a DNS entry, a place in the web server or Velocity configuration and in the certificate. Behind a proxy or load balancer, list it insite.ini [HTTPHeaderSettings] TrustedProxies[], or the site sees the proxy's scheme and address.
Common questions
Does the order of MatchOrder matter here? Yes. With uri;host, an address whose first element is a siteaccess
name (/admin) is matched by the URI probe on every host; everything else falls through to the host map. With
host;uri, a listed host always wins.
Do I need one map entry per language? With host matching alone, yes, one host per siteaccess. For one host with
a path per language, use host_uri (lesson 2.3).
Can the port be part of an entry? Yes, an entry may name one (example.com:8080); otherwise the port of the
request is ignored.
Why start for test systems? So that www.example.com.test.local reaches the same siteaccess as
www.example.com without a second entry per test host.
Pitfalls
- Shared lab. The host map and
MatchOrderlive in the global override. On a shared training server, let one person change them on the projector instead. - Browsers and
.localhost. Many current browsers send names ending in.localhostto the own computer; if a learner's does not, usecurlonly. - Forgetting the INI cache. Each change needs
--clear-tag=ini(and a Velocity restart when Velocity serves the lab).
Hands-on exercise
Use your training installation from lesson 2.1. Replace http://127.0.0.1:8087 with your lab's address.
Read the current probes:
php bin/php/console exp:ini get site.ini/SiteAccessSettings/MatchOrder.Add the host probe after the URI probe, keeping the URI probe first:
php bin/php/console exp:ini set site.ini/SiteAccessSettings/MatchOrder 'uri;host' global --dry-run php bin/php/console exp:ini set site.ini/SiteAccessSettings/MatchOrder 'uri;host' globalMap a host to the second site:
php bin/php/console exp:ini add 'site.ini/SiteAccessSettings/HostMatchMapItems[]' 'site-b.localhost;site_b' global php bin/php/ezcache.php --clear-tag=iniTest without DNS:
curl -s -H 'Host: site-b.localhost' http://127.0.0.1:8087/ | grep -i '<title>' curl -s http://127.0.0.1:8087/ | grep -i '<title>'Try a test host:
curl -s -H 'Host: site-b.localhost.training' http://127.0.0.1:8087/ | grep -i '<title>'. Note which site answers.Make the entry match by the start of the host:
php bin/php/console exp:ini rem 'site.ini/SiteAccessSettings/HostMatchMapItems[]' 'site-b.localhost;site_b' global php bin/php/console exp:ini add 'site.ini/SiteAccessSettings/HostMatchMapItems[]' 'site-b.localhost;site_b;start' global php bin/php/ezcache.php --clear-tag=iniRepeat step 5.
Repeat step 4 with the header
Host: SITE-B.localhostand withHost: site-b.localhost:8087.If your browser opens
http://site-b.localhost:8087/, open it and compare with the main site.Write down the three things a real host would also need before going live.
Expected result
- Step 4: the first command prints the title of Site B, the second the title of the main site.
- Step 5 answers with the default siteaccess; after step 6, step 7 answers with Site B.
- Step 8: both variants reach Site B, because case and the request's port are ignored.
- Step 10: a DNS entry, the web server or Velocity configuration, and the certificate (plus
SiteURL). - The trainer checks by running step 7 on one learner's lab on the projector.
Quiz
- Which setting maps
www.example.orgto the siteaccesssite_org? - An entry is
www.example.com;site;start. Which host does it match? A.preview.www.example.comB.www.example.com.test.localC.example.comD.www.example.org - Why is
partrisky on a server that answers for any host? - A browser asks for
WWW.Example.com.(capitals and a trailing dot). Does astrictentry forwww.example.commatch? - Name two things besides the host map that a new host name needs.
Answers
HostMatchMapItems[]=www.example.org;site_orgin[SiteAccessSettings], withHostMatchType=mapandhostinMatchOrder.- B.
startmatches a host that begins with the entry (Siteaccess by host prefix). - It also takes hosts you did not mean (
a.www.example.com.b); use it only where the web server answers for your own hosts. - Yes: hosts are compared without regard to case and to the trailing dot.
- Any two of:
SiteURLof the siteaccess, a DNS entry, the web server or Velocity configuration, the certificate,TrustedProxies[]behind a proxy.
Further reading
- Siteaccess by host prefix, and the redirect to the language.
- Multisite on one installation: host matching for hosts.
- Going live: hosts, HTTPS and siteaccesses.
- 8. Serving the site: web server and Velocity configuration.
Related pages
- Module 2: Matching requests to siteaccesses
- Multisite and site packages: course overview
- Trainers: start here
- Free documentation: this page is free to copy, translate and improve under the GNU FDL 1.3.
Become an Exponential Partner
Partners share their information, provide funding, contribute back and develop the products with us, and that is how Exponential grows. A partner website and dashboard are planned for mid-2027: please become a partner today to speed up our growth rate and land new features faster.