Jump to content

Generation of hreflang links for search engines as a module?


chrizz
 Share

Recommended Posts

hey there

I guess a lot of you have already heard of the hreflang attribute which tells search engines which URL they should list on their result pages. For some of my projects I build this manually but now I am wondering if there's need to add this as a module to PW modules directory. 

How do you deal with the hreflang thingy? Would you you be happy if you can use a module for this or do you have concerns that using a module maybe does not cover your current use cases?

Cheers,

Chris

 

 

 

 

Link to comment
Share on other sites

I may be missing something, but isn't this as simple as:

<?php
// handle output of 'hreflang' link tags for multi-language
// this is good to do for SEO in helping search engines understand
// what languages your site is presented in	
foreach($languages as $language) {
  // if this page is not viewable in the language, skip it
  if(!$page->viewable($language)) continue;
  // get the http URL for this page in the given language
  $url = $page->localHttpUrl($language); 
  // hreflang code for language uses custom lang_code field
  $hreflang = $language->lang_code; // /en or /pt in this website
  // output the <link> tag: note that this assumes your language names are the same as required by hreflang. 
  echo "\n\t<link rel='alternate' hreflang='$hreflang' href='$url' />";
}
?>

 

Link to comment
Share on other sites

@Sergio yeah, it's not that complicated - therefore I asked how you handle this before submitting a module ;)

Regarding your snippet:

imho "x-default" is missing here. If you don't want you site to be listed in other languages/regions as defined than your snippets works fine but as soon as you have a default language for the world's audience this won't work. 

May I ask where this comes from?

$language->lang_code;

Maybe I have some weird old setup but this variable is empty 

 

Link to comment
Share on other sites

Hey @chrizz.

I'll read more about x-default, thanks!

About the lang_code field. It's a custom field that I add to all my multilanguage projects. This is useful because I set the homepage path to be "/" to the default language so in the hreflang value, I output the real lang code, like "en" or "en_US" for instance. 

Link to comment
Share on other sites

Thanks! I've read that page and this one: https://www.rebelytics.com/hreflang-annotations/#step5

In the website I mentioned, I have the homepage as "/" and hreflang of "en". So I'll add a third hreflang tag with x-default value. The other language it "/pt". The user is not redirect upon arrival, he/she must select the language. 

<link rel='alternate' hreflang='x-default' href='http://example.com/' />
<link rel='alternate' hreflang='en' href='http://example.com/' />
<link rel='alternate' hreflang='pt' href='http://example.com/pt/' />

Actually, after reading it again, in this case I think I should have only to values: 

<link rel='alternate' hreflang='x-default' href='http://example.com/' /> <!-- default language is english for all users -->
<link rel='alternate' hreflang='pt' href='http://example.com/pt/' /> 

 

Edited by Sergio
Correction
  • Like 1
Link to comment
Share on other sites

I think these two lines would be fine. I guess you don't have the need for an additional module ;) 

but maybe someone else will join the discussion with a different approach/opinion on that?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...