Skip to content

Commit

Permalink
fix: SEO - correct canonical and alternate links (#97)
Browse files Browse the repository at this point in the history
* header
  • Loading branch information
salza80 authored Apr 22, 2024
1 parent 73a5128 commit f9a8d08
Showing 1 changed file with 71 additions and 3 deletions.
74 changes: 71 additions & 3 deletions src/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,79 @@
<meta property="og:title" content="<%= content_for?(:title) ? t('title_with_content', content: yield(:title)) : t('default_title') %>" />
<meta property="og:description" content="<%= content_for?(:description) ? yield(:description) : t('default_description') %>" />
<meta property="og:image" content="https://schengen-calculator.com/med.png" />
<%= canonical_tag -%>
<link rel="alternate" href="https://schengen-calculator.com" hreflang="en-us" />


<% canonical_tag = 'https://' + request.host_with_port + request.path %>
<% locales_pattern = I18n.available_locales.map(&:to_s).join('|') %>
<% alternate_path = '' %>
<% alternate_path2 = '' %>
<% alternate_path3 = '' %>
<% alternate_path_other_lang = '' %>
<% if request.path == '/en' || request.path == '/' || request.path == '/en/about' || request.path == '/about' %>
<% canonical_tag = 'https://' + request.host_with_port + '/' %>
<% alternate_path = 'https://' + request.host_with_port + '/about' %>
<% alternate_path2 = 'https://' + request.host_with_port + '/en/about' %>
<% alternate_path3 = 'https://' + request.host_with_port + '/en' %>
<% elsif request.path.starts_with?('/en/') %>
<% canonical_tag = 'https://' + request.host_with_port + request.path.sub('/en/', '/') %>
<% alternate_path2 = 'https://' + request.host_with_port + request.path %>
<% elsif request.path =~ /^\/(#{locales_pattern})(\/.*)?$/ %>
<% canonical_tag = 'https://' + request.host_with_port + request.path %>
<% alternate_path_other_lang = canonical_tag %>
<% if canonical_tag.end_with?('/about') %>
<% alternate_path2 = canonical_tag %>
<% canonical_tag = canonical_tag.sub(/\/about\z/, '') %>
<% alternate_path_other_lang = canonical_tag %>
<% end %>
<% if canonical_tag =~ /\/(#{locales_pattern})\z/ %>
<% alternate_path2 = canonical_tag + '/about' %>
<% end %>
<% else %>
<% alternate_path2 = 'https://' + request.host_with_port + '/en' + request.path %>
<% end %>

<link rel="canonical" href="<%= canonical_tag %>" hreflang="<%= locale.to_s %>" />
<% if alternate_path.present? %>
<link rel="alternate" href="<%= alternate_path %>" hreflang="<%= locale.to_s %>"/>
<% end %>
<% if alternate_path2.present? %>
<link rel="alternate" href="<%= alternate_path2 %>" hreflang="<%= locale.to_s %>"/>
<% end %>
<% if alternate_path3.present? %>
<link rel="alternate" href="<%= alternate_path3 %>" hreflang="<%= locale.to_s %>"/>
<% end %>

<% I18n.available_locales.each do |locale| %>
<link rel="alternate" href="https://schengen-calculator.com/<%= locale.to_s %>" hreflang="<%= locale.to_s %>" />
<% if locale != I18n.locale %>
<% if alternate_path3.present? %>
<% path_only = URI.parse(alternate_path3).path %>
<% new_path = path_only.sub(/^\/(#{locales_pattern})(\/.*)?$/, "/#{locale}\\2") %>
<% alternate_href_locale = alternate_path3.sub(path_only, new_path) %>

<link rel="alternate" href="<%= alternate_href_locale %>" hreflang="<%= locale.to_s %>" />
<% end %>

<% if alternate_path_other_lang.present? %>
<% path_only = URI.parse(alternate_path_other_lang).path %>
<% new_path = path_only.sub(/^\/(#{locales_pattern})(\/.*)?$/, "/#{locale}\\2") %>
<% alternate_href_locale = alternate_path_other_lang.sub(path_only, new_path) %>

<link rel="alternate" href="<%= alternate_href_locale %>" hreflang="<%= locale.to_s %>" />
<% end %>

<% if alternate_path2.present? %>
<% path_only = URI.parse(alternate_path2).path %>
<% new_path = path_only.sub(/^\/(#{locales_pattern})(\/.*)?$/, "/#{locale}\\2") %>
<% alternate_href_locale = alternate_path2.sub(path_only, new_path) %>

<link rel="alternate" href="<%= alternate_href_locale %>" hreflang="<%= locale.to_s %>" />
<% end %>
<% end %>
<% end %>




<title><%= content_for?(:title) ? t('.title_with_content', content: yield(:title)) : t('default_title') %></title>
<%= favicon_link_tag asset_path('favicon.ico') %>
<%= stylesheet_link_tag 'application', media: 'all' %>
Expand Down

0 comments on commit f9a8d08

Please sign in to comment.