Inserts a canonical url with a predefined host to prevent
search engines from crawling domains like herokuapp.com
git clone [email protected]:renuo/rack-canonical-header.git
cd rack-canonical-header
bin/setup
Add this line to your application's Gemfile:
gem 'rack-canonical-header'
And then execute bundle install
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.
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