Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow redirections to external URLs #1844

Open
Lucidiot opened this issue May 2, 2022 · 8 comments
Open

Allow redirections to external URLs #1844

Lucidiot opened this issue May 2, 2022 · 8 comments

Comments

@Lucidiot
Copy link

Lucidiot commented May 2, 2022

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
Copy link
Collaborator

Keats commented May 2, 2022

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

@Lucidiot
Copy link
Author

Lucidiot commented May 3, 2022

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
Copy link
Contributor

kartva commented May 16, 2022

pub fn make_permalink(&self, path: &str) -> String {
let trailing_bit =
if path.ends_with('/') || path.ends_with(&self.feed_filename) || path.is_empty() {
""
} else {
"/"
};
// Index section with a base url that has a trailing slash
if self.base_url.ends_with('/') && path == "/" {
self.base_url.clone()
} else if path == "/" {
// index section with a base url that doesn't have a trailing slash
format!("{}/", self.base_url)
} else if self.base_url.ends_with('/') && path.starts_with('/') {
format!("{}{}{}", self.base_url, &path[1..], trailing_bit)
} else if self.base_url.ends_with('/') || path.starts_with('/') {
format!("{}{}{}", self.base_url, path, trailing_bit)
} else {
format!("{}/{}{}", self.base_url, path, trailing_bit)
}
}

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

@Keats
Copy link
Collaborator

Keats commented May 16, 2022

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

@kartva
Copy link
Contributor

kartva commented May 16, 2022

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

@Keats
Copy link
Collaborator

Keats commented Jan 12, 2023

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

@kartva
Copy link
Contributor

kartva commented Jan 13, 2023

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
Copy link
Collaborator

Keats commented Jan 13, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants