getzola / zola

A fast static site generator in a single binary with everything built-in. https://www.getzola.org
https://www.getzola.org
MIT License
13.75k stars 955 forks source link

Allow redirections to external URLs #1844

Open Lucidiot opened 2 years ago

Lucidiot commented 2 years ago

Bug Report

Environment

Zola version: 0.15.3

Expected Behavior

Tell us what should have happened.

When setting redirect_to on a section's _index.md to an external URL, the redirection page should redirect to the external URL.

Current Behavior

Tell us what happens instead of the expected behavior. If you are seeing an error, please include the full error message and stack trace. You can get the stacktrace of a panic by adding RUST_BACKTRACE=1 when running a zola command.

The site's base URL gets concatenated to the external URL: with a base URL at https://foo.com and redirect_to = "https://bar.com/something", the user gets redirected to https://foo.com/https://bar.com/something.

Step to reproduce

Please provide the steps to reproduce the issue. If the issue is hard to reproduce, please provide a sample repository or sample that triggers the bug.

  1. Set the site's base URL to https://foo.com

  2. Create an _index.md anywhere in a site's content, and set a redirection:

    +++
    redirect_to = "https://bar.com/something"
    +++
  3. Generate the site.

  4. Open the section that you created.

You will get redirected to https://foo.com/https://bar.com/something.

Keats commented 2 years ago

Wouldn't you want to use 301 or 302 in that case?

Lucidiot commented 2 years ago

Well if we could get the server to redirect a few URLs, we would, but in many cases such as using GitHub Pages, you just do not have control over the server's responses. Zola already has aliases and redirect_to to work around this and use a small webpage to redirect, and redirecting to an external URL instead of an internal one does not seem to make much of a difference.

I made a workaround by copying the built-in internal/alias.html template over to the site's templates and using a custom variable in [extra] instead of redirect_to to avoid getting the base URL appended. So I guess just not adding the site's base URL when the destination URL is already absolute would fix this?

kartva commented 2 years ago

https://github.com/getzola/zola/blob/ede306cdd766bc53c590d44791b535e3e3309780/components/config/src/config/mod.rs#L173-L194

This seems like the code responsible for adding the base URL. Adding a check for : for an absolute path should be easy enough.

Keats commented 2 years ago

It should be changed upstream of that function, not in there. This function is responsible for making permalinks in the current site.

kartva commented 2 years ago

Ah, sorry. That makes sense. Anyway, I'd be happy to tackle this issue if it gets approved.

Keats commented 1 year ago

Sorry for the very late reply. I think it would be ok to add

kartva commented 1 year ago

Huh, from what I can see in the code is that link parsing in articles is done by the pulldown-cmark library, and functions in Zola assume that the link they're getting needs to be appended to the base_url in most cases to work. Is that correct?

Keats commented 1 year ago

In this issue, you only need to handle the redirect on a section so the line you have to change is https://github.com/getzola/zola/blob/next/components/site/src/lib.rs#L1117 Make a check whether the URL is external (there is a function for that in the markdown subcrate which should be extracted to the utils one) and do not generate a permalink if that's the case.