.. index:: single: Routing; Matching on Hostname How to match a route based on the Host ====================================== .. versionadded:: 2.2 Host matching support was added in Symfony 2.2 You can also match on the HTTP *host* of the incoming request. .. configuration-block:: .. code-block:: yaml mobile_homepage: path: / host: m.example.com defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage } homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage } .. code-block:: xml AcmeDemoBundle:Main:mobileHomepage AcmeDemoBundle:Main:homepage .. code-block:: php use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; $collection = new RouteCollection(); $collection->add('mobile_homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:mobileHomepage', ), array(), array(), 'm.example.com')); $collection->add('homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:homepage', ))); return $collection; Both routes match the same path ``/``, however the first one will match only if the host is ``m.example.com``. Placeholders and Requirements in Hostname Patterns -------------------------------------------------- If you're using the :doc:`DependencyInjection Component` (or the full Symfony2 Framework), then you can use :ref:`service container parameters` as variables anywhere in your routes. You can avoid hardcoding the domain name by using a placeholder and a requirement. The ``%domain%`` in requirements is replaced by the value of the ``domain`` dependency injection container parameter. .. configuration-block:: .. code-block:: yaml mobile_homepage: path: / host: m.{domain} defaults: { _controller: AcmeDemoBundle:Main:mobileHomepage } requirements: domain: %domain% homepage: path: / defaults: { _controller: AcmeDemoBundle:Main:homepage } .. code-block:: xml AcmeDemoBundle:Main:mobileHomepage %domain% AcmeDemoBundle:Main:homepage .. code-block:: php use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Route; $collection = new RouteCollection(); $collection->add('mobile_homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:mobileHomepage', ), array( 'domain' => '%domain%', ), array(), 'm.{domain}')); $collection->add('homepage', new Route('/', array( '_controller' => 'AcmeDemoBundle:Main:homepage', ))); return $collection; .. _component-routing-host-imported: Adding a Host Regex to Imported Routes -------------------------------------------- You can set a host regex on imported routes: .. configuration-block:: .. code-block:: yaml # app/config/routing.yml acme_hello: resource: "@AcmeHelloBundle/Resources/config/routing.yml" host: "hello.example.com" .. code-block:: xml .. code-block:: php // app/config/routing.php use Symfony\Component\Routing\RouteCollection; $collection = new RouteCollection(); $collection->addCollection($loader->import("@AcmeHelloBundle/Resources/config/routing.php"), '', array(), array(), array(), 'hello.example.com'); return $collection; The host ``hello.example.com`` will be set on each route loaded from the new routing resource.