My site is both available as:
www.site.com
site.com
I want the www.site.com to be the primary site. In case a user visits the naked site the header should have a canonical link to the www site. Is there any easy way to do this in Rails? I should be done for all pages from a certain controller. (not all controllers since the site is multi tenant through subdomain: like client1.site.com etc...)
Note: This comment has been heavily edited upon clarification from the original poster
To set a canonical link in Rails, you can use the canonical-rails
gem: https://github.com/jumph4x/canonical-rails
Install the gem and then you can do:
rails g canonical_rails:install
Which will put a canonical_rails.rb
file into config/initializers
. Open that file and define your canonical url by adding this line:
config.host = 'www.yourapp.com'
Then, if you have an app/views/layouts/layout.html.erb
file or similar, you can add:
<%= canonical_tag -%>
to the <head
> portion of your view.