How to render a remote file with rails
I have the sitemap of www.mysite.com hosted on https://s3.amazonaws.com/mysite/sitema开发者_开发技巧ps/sitemap1.xml.gz
Is it possible to configure Rails (routes, controllers, ...) to render the file sitemap1.xml under www.mysite.com/sitemap1.xml.gz?
Thanks.
Ps. the reason why the sitemap is under AWS is this: https://github.com/kjvarga/sitemap_generator/wiki/Generate-Sitemaps-on-read-only-filesystems-like-Heroku
based on https://github.com/kjvarga/sitemap_generator/issues/173
I'm trying this...
in routes.rb
get 'sitemap(:id).:format.:compression' => 'sitemap#show'
in sitemap_controller.rb
class SitemapController < ApplicationController
def show
data = open("http://{ENV['AWS_BUCKET_PROD']}.s3.amazonaws.com/sitemaps/sitemap#{params[:id]}.xml.gz")
send_data data.read, :type => data.content_type
end
end
Also make sure that sitemap (index) file contains links to other sitemap files (sitemap1, sitemap2...) located at your site and not amazon.
Create a controller that would redirect to Amazon S3 file location and create a matching route for it.
routes.rb:
match 'sitemap1.xml.gz' => 'site_map#redirect'
site_map_controller.b:
class SiteMapController < ApplicationController
def redirect
redirect_to 'https://s3.amazonaws.com/mysite/sitemaps/sitemap1.xml.gz'
end
end
As I understand it, you are deploying to a read-only filesystem such as Heroku.
If so, here are some articles that will help:
- Creating a sitemap for Rails and Heroku: "To solve this, we wrote a quick controller action to create the sitemap on the fly, and then cached it using page caching."
- Rails Google Sitemaps on Heroku: shows how to use a controller to parse the external sitemap and then render the sitemap XML.
- Generating a sitemap.xml file on Heroku with Ruby on Rails: "Here is a description of how to dynamically generate an xml sitemap every time it is accessed."
精彩评论