Redirect to an external site on page load?

Hello, I am beginner to Zola, sorry if this question has been asked before I couldn’t find anything.
How do I redirect to an external url on page load? I want to create routes which immediately redirects to an external site on page load.
I tried doing

<div onload="javascript:window.location.replace('https://google.com'))">
</div>

and

<script type="text/javascript">
window.onload = function () {
  window.location.replace('https://google.com')
  window.location.href = "https://google.com"
}
</script>

Didn’t work but then later I created a shortcode redirector.html

<script>
window.onload = function() {
  window.location.href = '{{ url }}'
}
</script>

and used it like this

+++
+++

{{ redirector(url = "https://google.com") }}

This does seem to be redirecting but it encoded the url so it doesnt work.

https://&/#x2F;&#x2F;google.com

Nevermind I found it. I had to mark it “safe” in the second approach.
basically create a redirectory.html shortcode with this:

<script>
window.onload = function() {
  window.location.href = '{{ url | safe }}'
}
</script>

and then you can use it on any page like so:

+++
+++

{{ redirector(url ="https://google.com") }}

Even possible with HTML only!

1 Like