2

I want to generate URL with https:// as a prefix, using the Twig function url(). However, the generated URL is always http://.

I made the setup given here: https://symfony.com/doc/6.2/routing.html#forcing-https-on-generated-urls.

My config/packages/framework.yaml:

framework:                                                                                                                                                                             
    http_method_override: false                                                                             
    handle_all_throwables: true                                                                             
    ...

My config/routes.yaml:

controllers:                                         
    resource:                                        
        path: ../src/Controller/                     
        namespace: App\Controller                    
    type: attribute                                  
    defaults: [https] // note HTTPS here

I am using the PROD environment.

My .env file is:

APP_ENV=prod
APP_URL=https://something... // note HTTPS here

Cache has been cleared with prod option.

However, when I use from my twig template:

{{ url('my_route') }}

the generated route is correct but still in http (and not https).

Am I missing something ?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
jean553
  • 621
  • 10
  • 29

3 Answers3

1

Looking here: https://symfony.com/doc/current/routing.html#forcing-https-on-generated-urls you will see you missed the schemes key in your config. Here is the example given:

# config/routes/annotations.yaml
controllers:
    resource: '../../src/Controller/'
    type: annotation
    defaults:
        schemes: [https]

Or do the following on a per route basis:

    #[Route('/login', name: 'login', schemes: ['https'])]
Arleigh Hix
  • 9,990
  • 1
  • 14
  • 31
  • Thanks for the reply! I have just tried the exact solution you mentioned above for the yaml file; (in `config/routes.yaml` in my case) but still, the URL generated into the twig template with `{{ url('my_route', {'id': ...}, true ) }}` (from here: https://symfony.com/doc/current/reference/twig_reference.html#url) still has `http://` prefix. In fact, the content generated with that twig template is then sent by email and I now realize by checking the mail content "in HTML format" that there is no scheme at all in the generated URL (I have something like: `//my-domain.com/my-route`) – jean553 Jul 12 '23 at 19:36
  • I guess the "http://" prefix is added automatically by the mail client, as there is no scheme in the URL. I am going to try the twig function `absolute_url` or the second solution you suggest that adds the scheme on a per route basis. – jean553 Jul 12 '23 at 19:37
0

Check your web server (ngnix, apache, etc) configuration, it was using the port 80 internally

There a trick to test: comment out the listen 80, if your site is unavailable that means it uses the port 80

ping
  • 61
  • 4
  • Thanks for your reply. Indeed this is something to think about, especially when the final web server is behind a gateway, a proxy or a load balancer. Very often, the frontend proxy listens on HTTPS and then forward traffic to the final web server using HTTP only, because routing is made internally into a private isolated network behind that proxy. That was not my issue here, but that is something to consider when dealing with that kind of issue. – jean553 Jul 16 '23 at 17:26
0

Found it. Using {{ absolute_url(path('my_route')) }} fixed it. My link is rendered with the scheme https://.

Thanks all for your replies. I guess the fix I made from Arleigh Hix suggestion was also part of my problem.

jean553
  • 621
  • 10
  • 29