Skip to content

Instantly share code, notes, and snippets.

@dylanmei
Created January 27, 2011 01:57
Show Gist options
  • Save dylanmei/797921 to your computer and use it in GitHub Desktop.
Save dylanmei/797921 to your computer and use it in GitHub Desktop.
rack _escaped_fragment_ handling
# Make redirects for SEO needs
class SeoAssist
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if fragment = request.params["_escaped_fragment_"]
original_fullpath = request.fullpath
new_uri = uri_from_fragment(request.path, fragment)
env["PATH_INFO"] = new_uri.path
env["QUERY_STRING"] = new_uri.query
env["REQUEST_URI"] = request.fullpath
logger.info "Rack::SeoAssist #{original_fullpath} => #{request.fullpath}"
end
@app.call(env)
end
private
def uri_from_fragment(path, fragment)
path.chop! if path.last == '/'
URI.parse(path + fragment)
end
def logger
RAILS_DEFAULT_LOGGER || Logger.new(STDOUT)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment