No commit activity in last 3 years
No release in over 3 years
A canonical URL tag for better SEO and to prevent search engines from crawling different hosts
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.16
>= 0
~> 10.0
>= 0
~> 3.0
 Project Readme

rack-canonical-header

Inserts a canonical url with a predefined host to prevent search engines from crawling domains like herokuapp.com

Setup

git clone git@github.com:renuo/rack-canonical-header.git
cd rack-canonical-header
bin/setup

Installation

Add this line to your application's Gemfile:

gem 'rack-canonical-header'

And then execute bundle install

Usage

Set the CANONICAL_HOST env variable to the desired host (i.e. set it to the domain of your website like CANONICAL_HOST=example.com)

The gem will inject a header of the format Link: <http[s]://CANONICAL_HOST[/path]>; rel=canonical as soon as the env is present. The path and the scheme will be replaced with what was requested on the original domain.

Note that the gem does no redirects. Therefore, your non-canonical urls will still work if you access them directly. However, search engines will not list them.

Non-Rails frameworks

If you're using a framework which uses Rack (like Hanami) you have to require and insert the middleware Canonical::Middleware manually into the rack stack.

For example:

require 'rack/auth/basic'
require 'rack-canonical-header'

module Web
  class Application < Hanami::Application
    configure do
      middleware.use Canonical::Middleware
    end
  end
end