Url with trailing slash is redirected back to the host url

Viewed 48

i have sales channel with the domain "http://tld.de/staging" and only this domain, the only other sales channel is the default headless sales channel.

I created a custom route like that:

@Route("/path", name="frontend.path.index", options={"seo"="false"}, methods={"GET"})

And it works just fine when using the url http://tld.de/staging/path but when I use the url http://tld.de/staging/path/ (with trailing slash) it redirects me to http://tld.de/path

I already tried to add a second route for that method but that didn't work

@Route("/path/", name="frontend.path.index.trailing_slash", options={"seo"="false"}, methods={"GET"})

Did I miss something or is that just the default behaviour?

Additional information: The custom controller class extends the StorefrontController and there are not redirects happenening inside of the custom controller, I even added a dd() at the beginning of the method for testing. And If i add the trailing slash to the first route the url with the trailing slash works but the one without gets redirected instead.

1 Answers

The redirect of the url with the trailing slash to the one without is the default behavior of Symfony. If that redirect doesn't respect your base path, then it's probably an issue with your configuration.

However if you absolutely must serve both URLs without redirecting, then you could use wildcards to do so. Obviously this is unconventional but should work:

/**
 * @Route("/path{trailingSlash}", name="frontend.path.index", methods={"GET"}, defaults={"trailingSlash"="/"}, requirements={"trailingSlash"="[/]{0,1}"})
 */
Related