diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index fa576998f1..032e41ecef 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -140,7 +140,7 @@ impl HtmlHandlebars { } else { debug!( "HTML 'site-url' parameter not set, defaulting to '/'. Please configure \ - this to ensure the 404 page work correctly, especially if your site is hosted in a \ + this to ensure the 404 page works correctly, especially if your site is hosted in a \ subdirectory on the HTTP server." ); "/" @@ -348,6 +348,7 @@ impl HtmlHandlebars { root: &Path, handlebars: &Handlebars<'_>, redirects: &HashMap, + site_url: &Option, ) -> Result<()> { if redirects.is_empty() { return Ok(()); @@ -357,12 +358,19 @@ impl HtmlHandlebars { for (original, new) in redirects { log::debug!("Redirecting \"{}\" → \"{}\"", original, new); - // Note: all paths are relative to the build directory, so the + // Note: all original paths are relative to the build directory, so the // leading slash in an absolute path means nothing (and would mess // up `root.join(original)`). let original = original.trim_start_matches("/"); let filename = root.join(original); - self.emit_redirect(handlebars, &filename, new)?; + let regex = Regex::new(r##"(^/)"##).unwrap(); + let destination = if site_url.is_some() && site_url.as_ref() != Some(&"/".to_string()) && regex.is_match(new) { + let site_url_value = site_url.as_ref().unwrap().to_string(); + format!("{}{}", site_url_value.trim_end_matches("/"), new) + } else { + new.to_string() + }; + self.emit_redirect(handlebars, &filename, &destination)?; } Ok(()) @@ -535,8 +543,7 @@ impl Renderer for HtmlHandlebars { super::search::create_files(&search, &destination, &book)?; } } - - self.emit_redirects(&ctx.destination, &handlebars, &html_config.redirect) + self.emit_redirects(&ctx.destination, &handlebars, &html_config.redirect, &html_config.site_url) .context("Unable to emit redirects")?; // Copy all remaining files, avoid a recursive copy from/to the book build dir