Jump to content
chrizz

Generation of hreflang links for search engines as a module?

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

 

 

 

 

Share this post


Link to post
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' />";
}
?>

 

Share this post


Link to post
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 

 

Share this post


Link to post
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. 

Share this post


Link to post
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

Share this post


Link to post
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?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By franciccio-ITALIANO
      Hi, we can choose the "headline" and "title" and "summery" in panel page of processwire, but we can't write the "metadecriptions" and "tags".
       I can write mdescropt and tags in templates, but I've same templates for many articles... so, how I can change mdescription and tags?

      Thanks...
    • By sins7ven
      Hi community, I am wondering if its possible to display languages based on what host/domain has been requested.
      The use case is the following: I have one installation of PW - and within this installation I set up 5 different languages (default, english, spanish, french, dutch). Now I have 3 different domains (domainA.com, domainB.com, domainC.com) and I want to decide which languages to make available based on what domain has been navigated to. 
      For example:
      domainA.com
       - default (domainA.com)
       - english (domainA.com/en)
      domainB.com
        - default (domainB.com)
        - french (domainB.com/fr)
        - dutch (domainB.com/nl)
      domainC.com
        - default (domainC.com)
        - dutch (domainC.com/nl)
      So what I want to achieve is making only these particular languages selectable in the language switcher on the frontend. Since all pages share the same templates and overall site structure I don't see the benefits of running this installation as a multisite setup or would it be better to do so? But I assume that this would be difficult to handle since the default language might change as well. (On the french version of the page, french should be the default language and so on). Any suggestions how to accomplish that? Thanks!
    • By Leftfield
      Hi All 🙂

      How to append canonical URL to head from certain templates?

      Thanks!!!
    • By MateThemes
      Hello!
      I am fairly new to Processwire. In the past I used OctoberCMS for my project. But I have more and more multilanguage projects and OctoberCMS don't fit my needs anymore.
      My first question to the community is, is it possible to hide/disable content based on the language. I mean, can a content editor say in German I want this content block and in Englisch I wanna hide this content block, based on the same layout?
      Thanks for your answers.
    • By Marco Angeli
      Hi there,
      I added a ssl certificate to my site and I'd like to redirect every single http url to its new https version
      So I added this code in the .htacces file, after the RewriteEngine On :
      Redirect 301 /about https://www.mysite.it/about
      Unfortunately this is now working: I get the "too many redirects" error.
      The following code works, but it's a bulk redirection to the home page, something I don't want for SEO reasons (https://moz.com/blog/save-your-website-with-redirects😞
      RewriteCond %{HTTP_HOST} mysite\.it [NC]
      RewriteCond %{SERVER_PORT} 80
      RewriteRule ^(.*)$ https://www.mysite.it/$1 [R,L]
      Any suggestions?
×
×
  • Create New...