50% discount in conference replays
2020 and 2021 events
In English, French, German, Polish and Spanish

How to Force Routes to always Use HTTPS or HTTP

Edit this page

Warning: You are browsing the documentation for Symfony 2.4, which is no longer maintained.

Read the updated version of this page for Symfony 6.0 (the current stable version).

How to Force Routes to always Use HTTPS or HTTP

Sometimes, you want to secure some routes and be sure that they are always accessed via the HTTPS protocol. The Routing component allows you to enforce the URI scheme via schemes:

  • YAML
  • XML
  • PHP
    path:     /secure
    defaults: { _controller: AcmeDemoBundle:Main:secure }
    schemes:  [https]

The above configuration forces the secure route to always use HTTPS.

When generating the secure URL, and if the current scheme is HTTP, Symfony will automatically generate an absolute URL with HTTPS as the scheme:

{# If the current scheme is HTTPS #}
{{ path('secure') }}
{# generates /secure #}

{# If the current scheme is HTTP #}
{{ path('secure') }}
{# generates https://example.com/secure #}

The requirement is also enforced for incoming requests. If you try to access the /secure path with HTTP, you will automatically be redirected to the same URL, but with the HTTPS scheme.

The above example uses https for the scheme, but you can also force a URL to always use http.


The Security component provides another way to enforce HTTP or HTTPS via the requires_channel setting. This alternative method is better suited to secure an "area" of your website (all URLs under /admin) or when you want to secure URLs defined in a third party bundle (see How to Force HTTPS or HTTP for different URLs for more details).

This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.