views:

92

answers:

2

Hello,

we are making a website that takes a generated incoming link and forwards the user who is clicking on it to another website while saving a record of the action in our DB. I guess it's basically what ad-services like AdSense do.

However, what is the best way to redirect the user?

I think html-meta-tag-redirects are out of question.

So what other options are there?

head :moved_permanently, :location => "http://www.domain.com/"

This one is a 301-redirect. The next one is a 302:

redirect_to "http://www.domain.com"

Are there any others? And which is best to use for our case? The links are highly-dynamic and change all the time.

We want to make sure we don't violate any existing standards and of course we don't want search-engines to tag us as spammers (which we are not, btw).

Thanks!

+5  A: 

From the browser/end user point of view

redirect_to "http://www.domain.com"
redirect_to "http://www.domain.com", :status => 302
redirect_to "http://www.domain.com", :status => 301

are equivalent to

head 301, :location => "http://www.domain.com/"
head 302, :location => "http://www.domain.com/"

There are some minor technical differences which can lead to one choice rather than the other.

redirect_to exists as part of the routing architecture. You can pass URL options and the method automatically creates the final location, according to the app routing rules.

redirect_to root_url, :status => 302
redirect_to { :controller => "main", :action => "index" }, :status => 302

On the opposite, head is a lower-level API for dealing with response headers. It doesn't care about the meaning of the headers you are passing to the response. It's useful when you specifically need to work with headers. I wouldn't use it to setup redirects.

Simone Carletti
A: 

Hi,

What if I need to add a custom http header variable while redirecting ? For istance I need to inject http basic auth header variable for testing piurpose.

Thanks for your answer !

Alexandre Mazari