symfony-cmf / routing-bundle

Symfony bundle to provide the CMF chain router to handle multiple routers, and the dynamic router to load routes from a database or other sources.
160 stars 78 forks source link

Redirection paths for multiple domains #451

Closed spointecker closed 3 years ago

spointecker commented 3 years ago

Hi @dbu

we are using the bundle only for making redirections within Symfony without having to add code.

Our application is used with different domains but only one database, therefore we use one redirect_route table for all redirections.

We ran into an issue while we tried to have the same staticPrefix for different host names. Is this bundle able to redirect to different uri with the same staticPrefix given, according to the hostname?

Example data:

---
uri: https://www.domain1.com/asdfasdf
staticPrefix: /test
host: www.domain1.com
----
uri: https://www.domain2.com/differentadsfasdf
staticPrefix: /test
host: www.domain2.com
---

With this sample data, only one redirection would work, the other throws a 404.

Symfony: 4.4 routing-bundle: 2.2.*

dbu commented 3 years ago

the static prefix is about how to store the redirection definitions in the content repository. as long as you don't have name clashes, sharing the prefix should work. if you have name clashes, you are overwriting the document and the last added redirect is the only one that exists.

do your routes set the host parameter? if they don't, they could match paths from other domains.

spointecker commented 3 years ago

Yes we set the host parameter (see example above). I get the feeling that we maybe use it "wrong", although it is working for our needs. (redirections only) Maybe you can give me an example how I would configure an example redirect for a given slug to a specific url? (is staticPrefix the right column?)

if they don't, they could match paths from other domains.

Ok, but is it possible to match the same path for different domains?

dbu commented 3 years ago

static prefix

sorry, i mixed up what static prefix is... https://github.com/symfony-cmf/routing-bundle/blob/e7f55a66d1279e9a141690acda061339964a365b/src/Model/Route.php#L40-L41 - this is an implementation detail to help find possibly matching routes. but you don't need to call that method, it should be automatically handled by a listener. is the listener not working in your setup, or are you calling setStaticPrefix manually?

host

the cmf routes are "normal" routes, just not created on the fly in PHP or yaml/xml code, but loaded from a database. if they specify a domain to match, that domain is used, if they don't limit to a domain, they will always match (and the redirection target should stay on the domain that was requested, if i am not mistaken)

you find some examples how redirect routes can be created in this fixture file of the cmf sandbox, maybe that helps you? https://github.com/symfony-cmf/cmf-sandbox/blob/a479131c40ff8af77794d6891096ba74d5346634/src/AppBundle/DataFixtures/PHPCR/LoadRoutingData.php#L160

spointecker commented 3 years ago

Thanks for your help @dbu ! I got it working by adding a "random" route_name.