2010-05-05 16:25:07 +00:00
|
|
|
module Apache
|
|
|
|
module Rewrites
|
|
|
|
def enable_rewrite_engine(options)
|
|
|
|
rewrite_engine! :on
|
|
|
|
options.each do |option, value|
|
|
|
|
case option
|
|
|
|
when :log_level
|
|
|
|
rewrite_log_level! value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewrites(&block)
|
|
|
|
self + indent(RewriteManager.build(&block))
|
|
|
|
end
|
2010-05-05 18:39:36 +00:00
|
|
|
|
|
|
|
def r301(*opt)
|
|
|
|
self << "Redirect permanent #{quoteize(*opt) * " "}"
|
|
|
|
end
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class RewriteManager
|
|
|
|
class << self
|
|
|
|
attr_accessor :rewrites
|
|
|
|
|
|
|
|
def build(&block)
|
|
|
|
@rewrites = []
|
|
|
|
|
|
|
|
self.instance_eval(&block)
|
|
|
|
|
|
|
|
@rewrites
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewrite(*opts)
|
|
|
|
@rewrite = RewriteRule.new
|
|
|
|
@rewrite.rule(*opts)
|
|
|
|
|
|
|
|
@rewrites << @rewrite
|
|
|
|
end
|
|
|
|
|
2010-05-05 18:39:36 +00:00
|
|
|
def r301(*opts)
|
|
|
|
redirect = RedirectMatchPermanent.new
|
|
|
|
redirect.rule(*opts)
|
|
|
|
|
|
|
|
@rewrites << redirect
|
|
|
|
end
|
|
|
|
|
2010-05-05 16:25:07 +00:00
|
|
|
def rewrite_test(from, to, opts = {})
|
|
|
|
orig_from = from.dup
|
|
|
|
@rewrites.each do |r|
|
|
|
|
from = r.test(from, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
if from != to
|
|
|
|
puts "[warn] #{orig_from} >> #{to} failed!"
|
|
|
|
puts "[warn] Result: #{from}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-05 18:39:36 +00:00
|
|
|
class MatchableThing
|
|
|
|
def tag; raise 'Override this method'; end
|
|
|
|
|
2010-05-05 16:25:07 +00:00
|
|
|
def initialize
|
|
|
|
@from = nil
|
|
|
|
@to = nil
|
|
|
|
@options = nil
|
|
|
|
@conditions = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def rule(from, to, *opts)
|
|
|
|
@from = from
|
|
|
|
@to = to
|
|
|
|
@options = opts
|
|
|
|
end
|
|
|
|
|
|
|
|
def test(from, opts)
|
|
|
|
from = from.gsub(@from, @to.gsub(/\$([0-9])/) { |m| '\\' + $1 })
|
|
|
|
opts.each do |opt, value|
|
|
|
|
from = from.gsub('%{' + opt.to_s.upcase + '}', value)
|
|
|
|
end
|
|
|
|
from
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
output = []
|
|
|
|
|
|
|
|
@conditions.each do |condition|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2010-05-05 18:39:36 +00:00
|
|
|
output << "#{tag} #{[@from.source, @to, @options].flatten * " "}"
|
2010-05-05 16:25:07 +00:00
|
|
|
|
|
|
|
output * "\n"
|
|
|
|
end
|
2010-05-05 18:39:36 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
class RewriteRule < MatchableThing
|
|
|
|
def tag; 'RewriteRule'; end
|
|
|
|
end
|
|
|
|
|
|
|
|
class RedirectMatchPermanent < MatchableThing
|
|
|
|
def tag; 'RedirectMatch permanent'; end
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
|
|
|
end
|