开发者

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

开发者 https://www.devze.com 2023-01-29 03:28 出处:网络
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 ow

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 a开发者_如何学Pythonny 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.


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!


Try using the refraction gem to redirect all accesses not going to a single defined host to that single defined host. This will also increase page ranking as it removes duplicate content:

# config/initializers/refraction_rules.rb
Refraction.configure do |req|
  if req.host == "www.you-like-this-host.com"
    # configure other redirects here
    # eg. I'm using this to migrate URLs from the legacy application
    # to this new application (eg when migrated from PHP to Rails)
  else
    # this is not your hostname, redirect!
    req.permanent! :host => "www.you-like-this-host.com"
  end
end

Refraction runs early in the rack stack so it's pretty lightweight. Apache's mod_rewrite won't work reliably with passenger for example, so I prefer refraction.

Be sure to load as a middleware plugin:

# config/environments/production.rb
config.middleware.insert_before ::Rack::Lock, ::Refraction, {}

Links with host name should never appear in the page cache. Are you sure you generate your in-site URLs without the host name? Maybe you tried to create unique links this way (by passing the current host name to link_to or url_for), better use refraction for that and cleanup your code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号