Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. # == How To
  2. #
  3. # We are going to assume your current working directory is the Rails
  4. # root and this file is named mongrel_rewrite.rb in a directory 'dev'
  5. # off of that root.
  6. #
  7. # $ mongrel_rails start -S dev/mongrel_rewrite.rb
  8. #
  9. # That's it!
  10.  
  11. #
  12. # These are our mongrel_rewrite rules. Obviously, add rules to
  13. # the RewriteRules hash with the key as the regex to match and the
  14. # value as the replacement string.
  15. #
  16. module ::Mongrel
  17. RewriteRules = {
  18. /\/(.+)\/([^.]+)\.\d+\.(swf|js|css|gif|jpg|png)/ => '/$1/$2.$3',
  19. }
  20. end
  21.  
  22. #
  23. # Enable really basic mod_rewrite style goodness with Mongrel.
  24. #
  25. class ::Mongrel::Rails::RailsHandler < ::Mongrel::HttpHandler
  26. alias :old_process :process
  27.  
  28. # Enable rewriting.
  29. def process(request, response)
  30. if ::Mongrel.const_defined?(:RewriteRules) && RewriteRules.is_a?(Hash)
  31. path = request.params[Const::PATH_INFO].dup
  32.  
  33. RewriteRules.dup.each do |rule, rewrite|
  34. if matches = rule.match(path).to_a[1..-1]
  35. rewrite = rewrite.dup
  36. while matches.size.nonzero? do
  37. rewrite.sub!(/\$(\d+)/, matches.shift)
  38. end
  39. path = rewrite
  40. end
  41. end
  42.  
  43. request.params[Const::PATH_INFO] = path
  44. end
  45. old_process(request, response)
  46. end
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement