Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a custom querystring to response #39

Open
oldtinroof opened this issue Jan 25, 2013 · 1 comment
Open

Adding a custom querystring to response #39

oldtinroof opened this issue Jan 25, 2013 · 1 comment

Comments

@oldtinroof
Copy link

I've got a simple 301 redirect to capture all non .com domains I have registered for my site as follows:

DOMAIN = 'www.mywebsite.com'

use Rack::Rewrite do
  r301 %r{.*}, "http://#{DOMAIN}$&", :if => Proc.new {|rack_env|
    rack_env['SERVER_NAME'] != DOMAIN && ENV["RACK_ENV"] == 'production'
  }
end

I'd like to add a querystring to the response to add the original requested domain in the format so that I can track what domains people are hitting.

?utm_source=#{rack_env['SERVER_NAME']}
But can't quite work out how not to crash the server :) Can it be done & retain any original query string?

It's unlikely that anyone will hit any subpages under the main domain, but when I drop the $& from the rewrite, and replace it with my string, it blows up with no errors in the logs...

*nb: I asked this on SO a week ago with no replies, so I thought I'd post here.

@swrobel
Copy link
Contributor

swrobel commented Apr 25, 2013

Ok, this is ugly but it's the best I could come up with for my situation, which is similar:

r301 %r{.*(\?.*)?}, lambda { |match, rack_env|
      "/to?utm_source=whatevers#{match[1]}".gsub("utm_source=whatevers?", "utm_source=whatevers&")
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants