8000 Option to disable the automatic 301 redirection without trailing slash · Issue #29011 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
8000

Option to disable the automatic 301 redirection without trailing slash #29011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
arneee opened this issue Oct 28, 2018 · 16 comments
Closed

Option to disable the automatic 301 redirection without trailing slash #29011

arneee opened this issue Oct 28, 2018 · 16 comments

Comments

@arneee
Copy link
arneee commented Oct 28, 2018

Description

The automatic 301 redirection from /foo/ to /foo was introduced in Symfony 4.1. In previous Symfony versions this results in a 404 response.

This is a "magic" behaviour which can be unwanted in certain cases. For example it can break AJAX calls via HTTPS.

There should be an option to disable this behaviour and enforcing strict URL matching on the route, without any magic redirects behind.

@nicolas-grekas
Copy link
Member

I'm not sure the linked SO entry applies: the router doesn't redirect from https to http. And the top reply is right: you should fix your calls to send them to the correct URL. I'm not sure we should make the component more complex to support this option (I'm pretty sure we shouldn't actually ;) )

@arneee
Copy link
Author
arneee commented Oct 29, 2018

I'm not sure the linked SO entry applies: the router doesn't redirect from https to http.

In my case it did, since the smyfony app was called from a reverse proxy via HTTP, but the connection to the client was HTTPS.

And the top reply is right: you should fix your calls to send them to the correct URL. I'm not sure we should make the component more complex to support this option (I'm pretty sure we shouldn't actually ;) )

The reason I don't like it is the "magic" behaviour. If you don't randomly see the part of the documentation, there is no reason to expect this. Since the redirect is not logged anywhere, you might only realize if it breaks suddenly for whatever reason (AJAX calls, API clients which don't support a redirect, etc).

@nicolas-grekas
Copy link
Member
nicolas-grekas commented Oct 29, 2018

The behavior makes sense to me for user facing URLs. That's why it's been added I suppose.
Maybe we should remove the redirect when the Accept header doesn't mention "html"? Would it make sense?

@arneee
Copy link
Author
arneee commented Oct 29, 2018

Personally I don't like unexpected "magic" functions at all, in any case. For now I have used a compiler pass to revert to the "classic" UrlMatcher, but this is an ugly workaround.

public function process(ContainerBuilder $container)    {
        $router = $container->getDefinition("router.default");
        $options = $router->getArgument(2);
        $options["matcher_class"] = 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher';
        $options["matcher_base_class"] = 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher';
        $router->setArgument(2, $options);
}

@xabbuh
Copy link
Member
xabbuh commented Oct 29, 2018

In my case it did, since the smyfony app was called from a reverse proxy via HTTP, but the connection to the client was HTTPS.

Still, the request should be treated as secure by the application. Are you sure you properly configured the trusted hosts?

@arneee
Copy link
Author
arneee commented Oct 29, 2018

In my case it did, since the smyfony app was called from a reverse proxy via HTTP, but the connection to the client was HTTPS.

Still, the request should be treated as secure by the application. Are you sure you properly configured the trusted hosts?

Maybe I need to do some additional configuration on the Symfony side, but since the conenction from reverse proxy to symfony was "http", Symfony created a 301 redirect to "http://" which was not trusted by the client anymore.

@xabbuh
Copy link
Member
xabbuh commented Oct 29, 2018

And that's why it's important to properly tell Symfony about the reverse proxy as the proxy will send a special header telling the application about the secure connection allowing Symfony to treat the request as if it was actually encrypted.

@nicolas-grekas
Copy link
Member

I would personally be OK to consider deprecating the redirection when the Accept header is present but doesn't contain html. I would keep the current behavior when the header is missing or when html is accepted. And I would not like to add an option to control this. More options mean more complexity, not worth it here IMHO.

@Nek-
Copy link
Contributor
Nek- commented Nov 14, 2018

Just to add some more insights for this issue. Here is another use case: we want to hide some part of the application using a reverse proxy. This means we proxy http://some.domain/ to http://another.domain/custom/reserved/path.

Depending on the proxy you use (basically: works with nginx, fails with haproxy) you may experiment redirection issue, because (and it's pretty logical) it transform http://some.domain/ to http://another.domain/custom/reserved/path/, which leads to http://some.domain/custom/reserved/path (and 404 error, of course).

@B-Galati
Copy link
Contributor

IMHO, no real issue here as it is all infrastructure related problems.

But it would be cool if one could opt-out from the trailing slash redirection for any reasons.
For example twitter support both URL without redirection; but the one without trailing slash is the canonical.

@arechsteiner
Copy link

I'm experiencing exactly what @Nek- described. My Symfony application returns a fully qualified redirect for the trailing slash and thus leads the user onto the origin server. The same happens for redirectToRoute() so I've opened a new issue: #29456

The problem might just be the fact that Symfony insists on fully qualifying the redirect which makes the whole thing a lot less flexible for this kind of scenario.

@COil
Copy link
Contributor
COil commented Dec 14, 2018

Also noticed this as it brokes some tests in one of my application. But they were redirections tests, so it's wasn't a problem as the final destination URL was correct.

@enrico69
Copy link

Adding an optional parameter in the configuration would really add complexity? It would kind of keep the backward compatibility with some existing projects.

@nicolas-grekas
Copy link
Member

I completely lost here: automatic redirection as described in the original description is expected and exists since early versions of the router component (before 2.3).
A deeper issue found early in the discussion was a mix of misconfigured trusted proxies.
Now, some of you report newly broken tests, which is unrelated to the original issue.
I think this issue has been hijacked to a point where it doesn't describe anything.
Closing as such. Please open specific issues if you want any follow up.
Before doing so, please check #29575 and #29673 which look similar.

@iquito
Copy link
Contributor
iquito commented Jul 30, 2019

As an addition to the CompilerPass solution by @arneee , which I used - for Symfony 4.3 it does not work anymore because of changes to the Routing component, so it needs to be modified to use Symfony\Component\Routing\Matcher\CompiledUrlMatcher (and setting matcher_base_class has no effect anymore, so it should be removed):

public function process(ContainerBuilder $container): void
{
    $router = $container->getDefinition("router.default");
    $options = $router->getArgument(2);
    $options['matcher_class'] = CompiledUrlMatcher::class;
    $router->setArgument(2, $options);
}

Maybe that helps others to avoid my headaches of finding a solution ;-)

@sela
Copy link
sela commented Aug 10, 2019

And that's why it's important to properly tell Symfony about the reverse proxy as the proxy will send a special header telling the application about the secure connection allowing Symfony to treat the request as if it was actually encrypted.

I also have the issue that I get 301 redirect from https to http did anyone manage to sort it out?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants
0