Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a canonical url for a dynamically generated urls on rails?

I implemented canonical url for homepage as follows: I added this line

  <link rel='canonical' href='https://example.com/'> 

I added this line on index.html.erb in header section. It is working fine.

Now I have dynamic routes generating line as follows:

<a href "<%= search_equipments_path(:category_id => category.id) %>">

I tried with following:

<a rel='canonical' href="<%= search_equipments_path(:category_id => category.slug) %>">

But didn't work.

like image 490
user3576036 Avatar asked Oct 18 '25 14:10

user3576036


1 Answers

We can set canonical tags in rails this way. This rails code will output canonical tags for full current url. Add this in layout in HTML head section.

 <link rel="canonical" href="<%= url_for(:only_path => false) %>" />

You can also specify the default protocol to be use for this URL by using:

<link rel="canonical" href="<%= url_for(:only_path => false, :protocol => 'https') %>" />

For more options please refer this link

Hope this helps.

like image 146
Mayank Avatar answered Oct 20 '25 12:10

Mayank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!