Twig - Get URL for canonical tag

user1961082 picture user1961082 · Nov 3, 2013 · Viewed 8.1k times · Source

I'm looking to create a dynamic rel="canonical" tag in my application which pulls in the current URL but want to ensure any query parameters are removed. E.g http://www.example.com/test/?page=2 should have the canonical as http://www.example.com/test/, therefore {{ app.request.uri }} doesn't work as this pulls in ?page=2 as well.

Does anyone know how to pull in the absolute path of a page without the query parameters?

Answer

jamek picture jamek · Nov 4, 2013

This will work,

{{ url(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')) }}