How to change http code on symfony schema redirect

Viewed 143

I'm migrating an old application to Symfony, and for SEO reason need to keep part of it on HTTP and part on HTTPS.

I'am using schemes config key in routing.yaml:

route_name:
    path: ...
    schemes: [HTTP]

The problem I'm having is that Symfony redirects from HTTPS to HTTP via HTTP 301 Moved Permanently, but I would like it to redirect just temporarily because we are planning to fully switch to https in next year.

Is there a way to change the response code? Or is there a better way to keep part of the application on HTTP and part on HTTPS?

1 Answers

I was interested, so I looked into it. Unfortunately this seems a bit more difficult than what I would have expected.

The permanent redirections is hard coded, see:

https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Routing/RedirectableUrlMatcher.php#L35

No problem, you just have to replace the matcher with your own... BUT NO. The matcher is not injected into the router, but created during the router's warmup.

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Routing/Router.php#L270

At least the matcher_class options seems to be what we are looking for. Unfortunately that is also hard coded:

https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.xml#L66

It looks like you will have create a custom matcher (with permanent => false), and redefine the router config with a compiler pass.

Unusually complicated, even by Symfony standards.

Related