How to render a remote file with rails
Asked Answered
C

3

6

I have the sitemap of www.mysite.com hosted on https://s3.amazonaws.com/mysite/sitemaps/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

Covalence answered 30/7, 2011 at 12:51 Comment(5)
Why not just define a route to a controller that downloads that file and serves it as output?Gaol
Surely it's trivial, but could you provide an example? Thanks.Covalence
looks like someone already wrote an example in their answer ;) A redirect would be simpler actually.Gaol
Hi all, any news here? I have exactly the same problem and I can't find nothing around the web about this...Commutator
Did you find a solution? Sharing it here would be useful. (It is also ok to accept your own answer.)Dagan
E
3

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.

Enginery answered 20/10, 2015 at 18:45 Comment(3)
This is probably the only way. Excellent!Balcony
Can you kindly tell me that what is the reason behind this "Also make sure that sitemap (index) file contains links to other sitemap files (sitemap1, sitemap2...) located at your site and not amazon." ?Bhili
@Mohsin Sethi, it's been 5 years since I did it. So I don't know. You may not need it, or the reason you need it is the same as the reason you need to serve your index sitemap from your site and not from aws directly.Enginery
H
0

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
Harrold answered 30/7, 2011 at 13:49 Comment(3)
Although the url is correct s3 needs credentials. So this won't work.Zales
The problem is that Google is not happy if you serve a sitemap with a redirect, as the sitemap need to be served from within the mapped domain. Additionally, in this way you're exposing the s3 directory, which is not a best practice.Covalence
As Topo said a sitemap should not be after a redirect, this solution will not workTrampoline
D
0

As I understand it, you are deploying to a read-only filesystem such as Heroku.

If so, here are some articles that will help:

Dagan answered 17/7, 2012 at 2:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.