Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created May 30, 2014 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoward/43a1be3b77cac5fb9d43 to your computer and use it in GitHub Desktop.
Save ahoward/43a1be3b77cac5fb9d43 to your computer and use it in GitHub Desktop.
# config/initializers/trailing_slash.rb
Rails.configuration.before_initialize do
ActionController::Base.module_eval do
before_filter :enforce_trailing_slash
protected
def enforce_trailing_slash
ext = request.fullpath.split('.', 2)[1]
if ext.nil? and request.format.to_s == 'text/html'
url = request.original_url
if request.get? && !url.ends_with?('/')
redirect_to(url + '/')
return
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment