Created
January 27, 2011 01:57
-
-
Save dylanmei/797921 to your computer and use it in GitHub Desktop.
rack _escaped_fragment_ handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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