Created
December 30, 2009 03:28
-
-
Save mikldt/265819 to your computer and use it in GitHub Desktop.
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
class ApplicationController < ActionController::Base | |
# ... | |
######################## | |
# URL REDIRECTS # | |
######################## | |
# Use this method to make sure that each page can only be accessed | |
# by one URI. Will return false if a redirect has occured. | |
# Example (in controller) | |
# ensure_uri production_path(@production) or return | |
def ensure_uri(uri) | |
#These session variable just help us keep out of infinite loops. | |
unless session[:redirect_uri] == uri | |
session[:redirect_count]=0 | |
session[:redirect_uri] = uri | |
end | |
if uri != request.request_uri && session[:redirect_count]<2 | |
# Bad URI, need to redirect. | |
redirect_to uri, :status=>:moved_permanently | |
session[:redirect_count] += 1 | |
logger.info "Redirect for #{uri}: no. #{session[:redirect_count]}" | |
flash[:notice]= CGI.escapeHTML("You have been redirected from #{request.env["HTTP_HOST"]}#{request.request_uri} to this page's new location.") | |
false | |
else | |
# Landed at the right URI, with or without help from redirects. | |
session[:redirect_uri]='' | |
if session[:redirect_count] >= 2 | |
logger.warn "Too many redirects for #{uri}!!! Giving up on pretty URL with #{request.request_uri}" | |
end | |
true | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment