rust-lang / mdBook

Create book from markdown files. Like Gitbook but implemented in Rust
https://rust-lang.github.io/mdBook/
Mozilla Public License 2.0
17.71k stars 1.61k forks source link

Canonical Links Best Practice #1238

Open markhildreth opened 4 years ago

markhildreth commented 4 years ago

Is there a recommended practice for ensuring canonical URLs for a book? For example, the current rust book can be found at these URLs:

However, I don't notice a <link rel="canonical" href="https://doc.rust-lang.org/book/"> on any of the resulting documents. Should mdbook offer any recommendations in this regard, and/or a configuration option to allow for a canonical link on pages?

ehuss commented 4 years ago

I think it would probably use useful to have a canonical link. Would probably need to add a site-url to the config to specify the canonical URL. Would then need to feed that into the template (perhaps a computed value of site-url joined with the current page's canonical name).

(Note, also briefly discussed adding site-url at https://github.com/rust-lang/mdBook/pull/1221#issuecomment-635522550)

markhildreth commented 4 years ago

@ehuss Thanks for the quick response. Based on your response and some more digging, I see a few ways forward:

1.) Expose the rendered path (e.g., /index.html for the root/README.md) to the template. This is technically all we need: then the user could just create the full URL in their template with the rendered path. Something like....

<link rel="canonical" href="https://example.com{{ rendered_path }}" />

A bit hackish (especially with the "who is supposed to add the slash" confusion), but it works.

2.) In addition to the above, expose a canonical-url option (e.g., "https://example.com"). If this option is specified, then all pages will have the canonical link placed in their header by default, created by adding the rendered path onto the given canonical URL.

You mention a site-url, although I'm not sure that's what we want. For example, you may want to have a latest.example.com and an example.com. These might have different site-url values, but for SEO reasons might want to have the same canonical URL.

pinage404 commented 2 months ago

Hello,

I changed the domain name of a website generated by mdBook

Now, Google doesn't want to index the new URL because it is detected as duplicate content

Duplicate without user-selected canonical These pages aren't indexed or served on Google

Is there currently a solution ? (Even if it's a bit hacky)