|
| 1 | +.. index:: |
| 2 | + single: Routing; Scheme requirement |
| 3 | + |
| 4 | +How to force routes to always use HTTPS |
| 5 | +======================================= |
| 6 | + |
| 7 | +Sometimes, you want to secure some routes and be sure that they are always |
| 8 | +accessed via the HTTPS protocol. The Routing allows you to enforce the HTTP |
| 9 | +scheme via the ``_scheme`` requirement: |
| 10 | + |
| 11 | +.. configuration-block:: |
| 12 | + |
| 13 | + .. code-block:: yaml |
| 14 | +
|
| 15 | + secure: |
| 16 | + pattern: /secure |
| 17 | + defaults: { _controller: AcmeDemoBundle:Main:secure } |
| 18 | + requirements: |
| 19 | + _scheme: https |
| 20 | +
|
| 21 | + .. code-block:: xml |
| 22 | +
|
| 23 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 24 | +
|
| 25 | + <routes xmlns="http://symfony.com/schema/routing" |
| 26 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 27 | + xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd"> |
| 28 | +
|
| 29 | + <route id="secure" pattern="/secure"> |
| 30 | + <default key="_controller">AcmeDemoBundle:Main:secure</default> |
| 31 | + <requirement key="_scheme">https</requirement> |
| 32 | + </route> |
| 33 | + </routes> |
| 34 | +
|
| 35 | + .. code-block:: php |
| 36 | +
|
| 37 | + use Symfony\Component\Routing\RouteCollection; |
| 38 | + use Symfony\Component\Routing\Route; |
| 39 | +
|
| 40 | + $collection = new RouteCollection(); |
| 41 | + $collection->add('secure', new Route('/secure', array( |
| 42 | + '_controller' => 'AcmeDemoBundle:Main:secure', |
| 43 | + ), array( |
| 44 | + '_scheme' => 'https', |
| 45 | + ))); |
| 46 | +
|
| 47 | + return $collection; |
| 48 | +
|
| 49 | +The above configuration forces the ``secure`` route to always use HTTPS. |
| 50 | + |
| 51 | +When generating the ``secure`` URL, and if the current scheme is HTTP, Symfony |
| 52 | +will automatically generate an absolute URL with HTTPS as the scheme: |
| 53 | + |
| 54 | +.. code-block:: text |
| 55 | +
|
| 56 | + # If the current scheme is HTTPS |
| 57 | + {{ path('secure') }} |
| 58 | + # generates /secure |
| 59 | +
|
| 60 | + # If the current scheme is HTTP |
| 61 | + {{ path('secure') }} |
| 62 | + # generates https://example.com/secure |
| 63 | +
|
| 64 | +The requirement is also enforced for incoming requests. If you try to access |
| 65 | +the ``/secure`` path with HTTP, you will automatically be redirected to the |
| 66 | +same URL but with the HTTPS scheme. |
| 67 | + |
| 68 | +The above example uses ``https`` for the ``_scheme``, but you can also force a |
| 69 | +URL to always use ``http``. |
| 70 | + |
| 71 | +.. note:: |
| 72 | + |
| 73 | + The Security component provides another way to enforce the HTTP scheme via |
| 74 | + the ``requires_channel`` setting. This alternative method is better suited |
| 75 | + to secure an "area" of your website (all URLs under ``/admin``) or when |
| 76 | + you want to secure URLs defined in a third party bundle. |
0 commit comments