Skip to content

A Rack middleware to include automatically a canonical link into your headers

License

Notifications You must be signed in to change notification settings

renuo/rack-canonical-header

Repository files navigation

rack-canonical-header

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

Setup

git clone [email protected]: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

About

A Rack middleware to include automatically a canonical link into your headers

Resources

License

Stars

Watchers

Forks

Packages

No packages published