When calling {% redirect externalUrl %} from source mydomain.com/#anchor to externalUrl = destination.com the #anchor is appended to the externalUrl after the redirect (destination.com/#anchor).

Note that the code is called by a routing template in craft cms.

The externalUrl variable does not contain the anchor link fragment and I cannot find a way to prevent craft from adding it. The only way I succeeded was using javascript code, but I don't like that solution:

    window.location.href = '{{ externalUrl }}'; // this works
{% exit %}

I can remove the anchor fragment using this js code as well but for some reason it still appends to the redirect url in the end, maybe due to some timing issue:

    // this removes the anchor in the browser url field
    if (typeof window.history.replaceState == 'function') {
      history.replaceState({}, '', window.location.href.slice(0, -1));
{% redirect url %} {# not working, still adds anchor #}

How can I do this without js?

  • I have reproduced this... It seems like a Craft bug to me, probably worth reporting. Commented Mar 4 at 9:22


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.