jayd
2011-Oct-25 17:38 UTC
[Puppet Users] ERB: apache2 vhost template - iterating over array writing out mod_rewrite rules
Hi guys, I''m having a problem and maybe somebody might be able to help out with that. I have a plain simple erb template which I''m basically using for writing out my vhost files for apache2. Using mod_rewrite I want to control the redirect based on aliaes assigned to the vhost: -------------------------------8<--------------------------------- RewriteCond %{HTTP_HOST} ^vhost\.alias1\.tld [OR] RewriteCond %{HTTP_HOST} ^vhost\.alias2\.tld RewriteRule ^/(.*) http://target.website.tld/$1 [L,R=301] ------------------------------->8--------------------------------- Using this snippet works just fine but trying to cover it within an erb template while iterating over an array of possible aliases turned out to be much more complex then I first thought... ;) Using the following erb snippet ... -------------------------------8<--------------------------------- [...] <% if has_variable?("vhostaliaes_regex") then %> <% vhostaliases_regex.each do |Alias| %> RewriteCond %{HTTP_HOST} <%= Alias %> [OR] <% end %> RewriteRule ^/(.*) http://<%= servername %>/$1 [L,R=301] <% end %> [...] ------------------------------->8--------------------------------- ... will result in the following output: -------------------------------8<--------------------------------- RewriteCond %{HTTP_HOST} ^vhost\.alias1\.tld [OR] RewriteCond %{HTTP_HOST} ^vhost\.alias2\.tld [OR] RewriteRule ^/(.*) http://target.website.tld/$1 [L,R=301] ------------------------------->8--------------------------------- Because of the additional ''[OR]'' parameter at the end of the second line an endless loop is being created crashing all vhosts. Does anybody have an idea on how to deal with this? Thanks :) Cheers - Jan -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Nan Liu
2011-Oct-25 17:57 UTC
Re: [Puppet Users] ERB: apache2 vhost template - iterating over array writing out mod_rewrite rules
On Tue, Oct 25, 2011 at 5:38 PM, jayd <jan@agetty.de> wrote:> Hi guys, > > I''m having a problem and maybe somebody might be able to help out with > that. I have a plain simple erb template which I''m basically using for > writing out my vhost files for apache2. Using mod_rewrite I want to control > the redirect based on aliaes assigned to the vhost: > > -------------------------------8<--------------------------------- > RewriteCond %{HTTP_HOST} ^vhost\.alias1\.tld [OR] > RewriteCond %{HTTP_HOST} ^vhost\.alias2\.tld > RewriteRule ^/(.*) http://target.website.tld/$1 [L,R=301] > ------------------------------->8--------------------------------- > > Using this snippet works just fine but trying to cover it within an erb > template while iterating over an array of possible aliases turned out to be > much more complex then I first thought... ;) > > Using the following erb snippet ... > > -------------------------------8<--------------------------------- > [...] > <% if has_variable?("vhostaliaes_regex") then %> > <% vhostaliases_regex.each do |Alias| %> > RewriteCond %{HTTP_HOST} <%= Alias %> [OR] <% end %> > RewriteRule ^/(.*) http://<%= servername %>/$1 [L,R=301] > <% end %> > [...] > ------------------------------->8--------------------------------- > > ... will result in the following output: > > -------------------------------8<--------------------------------- > RewriteCond %{HTTP_HOST} ^vhost\.alias1\.tld [OR] > RewriteCond %{HTTP_HOST} ^vhost\.alias2\.tld [OR] > RewriteRule ^/(.*) http://target.website.tld/$1 [L,R=301] > ------------------------------->8--------------------------------- > > Because of the additional ''[OR]'' parameter at the end of the second line an > endless loop is being created crashing all vhosts. Does anybody have an > idea on how to deal with this?Try vhostaliases_regex.collect{ |val| "RewriteCond %{HTTP_HOST} #{val}"}.join(" [OR]\n")> vhostaliases_regex = [''alias1'', ''alias2'']=> ["alias1", "alias2"]> val = vhostaliases_regex.collect{ |val| "RewriteCond %{HTTP_HOST} #{val}"}.join(" [OR]\n")=> "RewriteCond %{HTTP_HOST} alias1 [OR]\nRewriteCond %{HTTP_HOST} alias2"> puts valRewriteCond %{HTTP_HOST} alias1 [OR] RewriteCond %{HTTP_HOST} alias2 => nil Thanks, Nan -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Jan
2011-Oct-27 08:52 UTC
Re: [Puppet Users] [SOLVED] ERB: apache2 vhost template - iterating over array writing out mod_rewrite rules
Hi Nan, On 10/25/2011 07:57 PM Nan Liu wrote: [...]>> Because of the additional ''[OR]'' parameter at the end of the second line an >> endless loop is being created crashing all vhosts. Does anybody have an >> idea on how to deal with this? > > Try vhostaliases_regex.collect{ |val| "RewriteCond %{HTTP_HOST} > #{val}"}.join(" [OR]\n") > >> vhostaliases_regex = [''alias1'', ''alias2''] > => ["alias1", "alias2"] >> val = vhostaliases_regex.collect{ |val| "RewriteCond %{HTTP_HOST} #{val}"}.join(" [OR]\n") > => "RewriteCond %{HTTP_HOST} alias1 [OR]\nRewriteCond %{HTTP_HOST} alias2" >> puts val > RewriteCond %{HTTP_HOST} alias1 [OR] > RewriteCond %{HTTP_HOST} alias2 > => nilwhy things cannot be always that easy.. that''s the snippet of erb code I made up: --------------------------------8<--------------------------------------- <% if vhostaliases_regex != "" then -%> <% vhostaliases_new = vhostaliases_regex.collect{ |val| "RewriteCond %{HTTP_HOST} #{val}"}.join(" [OR]\n ") %> <% vhostaliases_new.each do |Alias| %><%= Alias %><% end %> RewriteRule ^/(.*) http://<%= servername %>/$1 [L,R=301] <% end -%> -------------------------------->8--------------------------------------- Thanks :) - Jan