Search code examples
ruby-on-railspage-caching

My Rails page_cache is attaching someone else's domain to links, how can I stop this?


Let me try to explain what the situation is as thoroughly as I can see it.

I have a Rails app using page caching for a specific part of the site. When looking through Google Analytics I noticed my own domain as a referrer which I found odd. Upon further investigation I discovered that when I go to one of those pages with my domain as the referrer, the links point to a different domain name but renders the page fine. For example:

I'm on domain.com. I go to domain.com/someones_profile, and when I hover over any links it will say anotherdomain.com/someones_profile/about. Now, if I click the link, it'll take me to anotherdomain.com/someones_profile/about yet it's my page being accessed on my server (logs verify it.)

I tested this out by taking one of my unused domain names and changing the A record to my server's IP. Then I cleared the page cache directory and visited a cacheable page using the domain I just set up, domain2.com/someones_profile, and it cached the links as domain2.com/someones_profile.

Hopefully this explanation made sense. The domain that is "infiltrating" my cache belongs to someone I don't know, and the intent doesn't seem in any way malicious, but I was wondering if Rails has any sort of built-in methods to circumvent something like this. Something like forcing page_cache links to use a specific domain rather than whatever the referrer domain is.

Any help would be appreciated, I would prefer to use built-in Rails method than to write something myself, maybe/maybe not because I'm lazy.


Solution

  • Ok, I think I get your problem. How about you create your links in pages with xxx_path instead of xxx_url? This should cause only relative paths to be generated. No domain names will be written into the html document. I would do a search and replace through the views for _url and change them to _path.

    So for example, if you have this in your view:

    <%= link_to "View all posts", posts_url %>
    

    You'd change it to:

    <%= link_to "View all posts", posts_path %>
    

    By the way, this answer assumes that you're creating your links by using routes, not by putting a URL directly into the view!