6

Can I use:

<link rel="alternate" hreflang="en" href="?l=en" />

to link pages with different languages for SEO (recommended for example by Google)? Can that URL be relative as in the example above, or does it have to be full (start with http://example.com)?

8
  • Note: there is no closing slash on link tags in HTML. That tag is self closing and this is not XHTML.
    – Rob
    May 20, 2016 at 2:08
  • @Rob : According to Free Radical's answer to this question by CaptSaltyJack, it seems the closing slash is "optional". Although I'm in favor of using HTML which is not XHTML, I'm not offhand recalling any HTML specification that has forbidden a closing slash.
    – TOOGAM
    May 20, 2016 at 3:54
  • @TOOGAM I said nothing about it being forbidden. I am stating that the specification for this element in no way, shape or form, by words or any example anywhere, uses, implies or requires the closing slash. In fact, the spec specifically states browsers are to ignore the slash because it has no meaning, has no value and does nothing. Some people love to bring up the links your CaptSaltyJack has but, as he has, it points out exactly what I said. There is no specification, anywhere, ever, that shows the <link> element with a closing slash (nor any of the others such as <img>, <input>, etc.
    – Rob
    May 20, 2016 at 4:01
  • 1
    @Rob — Here's a spec that makes it mandatory, and heres another one that makes a reference to one which also makes it mandatory. Neither of them are likely to apply in this case, but they do exist.
    – Quentin
    May 20, 2016 at 9:34
  • 1
    I've used relative links for a while now, specifically for hreflang, and everything works perfectly
    – Martijn
    May 20, 2016 at 11:50

3 Answers 3

8

Looks like it. This example comes from the HTML5 specs:

For example, the following link is a French translation that uses the PDF format:

<link rel=alternate type=application/pdf hreflang=fr href=manual-fr>

3
  • 1
    The quote is correct, but its HTML syntax is wrong and potentially dangerous. Always quote your attributes.
    – ArtOfCode
    May 20, 2016 at 8:19
  • 3
    @ArtOfCode I would always recommend quoting HTML attributes as well, however, the above quoted HTML5 syntax is not "wrong" in this instance. The attribute values only need to be quoted if they contain spaces or a bunch of other special characters. (However, the slash is likely to trip up an HTML4 parser.)
    – MrWhite
    May 20, 2016 at 8:53
  • @w3dk Aye, it's just a matter of being careful. Without quotes it becomes much easier to write incorrect HTML by dropping a > where it shouldn't be.
    – ArtOfCode
    May 20, 2016 at 8:57
1

If possible do always use absolute links instead of relative ones.

Why?

Because relative links may cause crawl errors. Especially when it comes to alternate links you should make sure the bot finds exactly the URL you want it to crawl.

Further it is not a good idea to use parameters for language indication.

Please visit the following guides on multi language websites: https://support.google.com/webmasters/answer/182192?hl=en

And implementing hreflang: https://support.google.com/webmasters/answer/189077?hl=en

6
  • 6
    "Because relative links may cause crawl errors." - If a bot is unable to parse a relative URL (specifically where the spec states that relative URLs are permitted) then it's probably not a bot worth considering.
    – MrWhite
    May 19, 2016 at 23:27
  • 4
    Not that I disagree with you on that "it is not a good idea to use parameters for language indication", but I find it greatly amusing that the link that you provide on the topic does, itself, use a parameter for language indication.
    – Jase
    May 20, 2016 at 4:18
  • Both of you have a point, especially Google’s decision to use parameters.
    – Seb
    May 20, 2016 at 5:14
  • But I'm speaking from experience. Relative linking often causes weird URLs being crawled and indexed. So besides the official standard allowing relative links I won't recommend it.
    – Seb
    May 20, 2016 at 5:19
  • 1
    @Jase "Do as I say, not as I do"
    – user11153
    May 20, 2016 at 7:46
1

Yes.

The definition of the href attribute for the link element says:

[…] must contain a valid non-empty URL potentially surrounded by spaces

This links to the definition of valid non-empty URL, which links to the definition of valid URL, which says that is has to be a URL that

conforms to the authoring conformance requirements in the URL standard

That URL standard is http://www.w3.org/TR/url/ (which is actually only a Working Draft from 2014), and this, of course, defines/allows relative URLs.

Or in other words (tl;dr): The href attribute is the same for a, area, and link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.