Rewrite URL from Subdirectory to Subdomain in Rails for Heroku
I'm trying to rewrite:
mysite.com/blog to blog.mysite.com
To be clear: I only want the user to see mysite.com/blog .Where the blog is actually a separately hosted wordpress site at blog.mysite.com. And mysite.com is a Rails app.
I have attempted to use rack-rewrite to achieve this and I can make it work with a 301 redirect, but not with a rewrite.
config.middleware.insert_before(Rack::Lock, Rack::Rewrite) do
#r301 '/blog', 'http://blog.mysite.com' #works
rewrite '/blog', 'http://blog.mysite.com' #fails
end
The reason I am trying to do this in Rails and not in the webserver is because I am using Heroku for hosting and I believe it is not possible to configure this type of rewrite on Heroku.
So my question is simply, how can I achieve this rewrite?
ps I saw another post suggesting the use of rack-reverse-proxy but this gem seems quite old and doesn't seem to have had much development. Which makes me nervous of using it.
In the absence of an alternative solution, I set aside my reservations and used the rack-reverse-proxy gem. As suggested in some other posts (that SO doesn't let me link to here).
It's working fine for me (Ruby 1.9.3, Rails 3.2.12).
My code is:
use Rack::ReverseProxy do
reverse_proxy /^/blog(/.*)$/, 'http://blog.example.com$1', opts={:preserve_host => true} #works
end
At the bottom of the config.ru file.
Note: In Wordpress I also needed to change the Site URL in Settings > General. I changed it to: http://example.com/blog but I kept the .htaccess file unchanged.
A final point: all my CSS and JS were referenced OK, but my local fonts weren't. I solved this by referencing the fonts in the assets directory of my rails app (as opposed to in my Wordpress theme).
It sounds like your blog and your website are hosted in different places.
Rewrite will only work if you want to serve up the same rails page but from a different URL. It won't work between different hosts: https://github.com/jtrupiano/rack-rewrite#rewrite
The best solution is to be OK w/ users being redirected to a subdomain.
A couple other less ideal options:
So, I recommend just using the subdomain.
链接地址: http://www.djcxy.com/p/60104.html