How do I make a simple redirect site in ruby?

Is it possible to make a site that only preforms a redirect to another
site? Here’s an example:

Let’s just say that a netflix.com URL to a movie is something like
Netflix

Could you make a really simple app in Rails at say the domain
netflixit.com that allows people to just type in like netflixit.com/
fantasy/heroes and be instantly directed to the netflix version? So, I
guess it has to convert and change the URL, but I don’t know how. The
website would be pretty much blank. The only thing it would be used
for is automatically redirecting to the netflix site. The idea is just
to give people an easier way to find something in a database type site
if they already know what they are looking for.

This sounds like it would be very easy, but I’m still a beginner in
Rails. I don’t even know the correct term for what this would be
called - might be easier to find information if I did :smiley:

So, does anyone know any tutorials that show how to do this, or at
least know the technical term for this so I can do my own searching?

Thanks, guys!

MOLTEN wrote in post #965662:

Could you make a really simple app in Rails at say the domain
netflixit.com that allows people to just type in like netflixit.com/
fantasy/heroes and be instantly directed to the netflix version? So, I
guess it has to convert and change the URL, but I don’t know how. The
website would be pretty much blank. The only thing it would be used
for is automatically redirecting to the netflix site. The idea is just
to give people an easier way to find something in a database type site
if they already know what they are looking for.

This sounds like it would be very easy, but I’m still a beginner in
Rails. I don’t even know the correct term for what this would be
called - might be easier to find information if I did :smiley:

So, does anyone know any tutorials that show how to do this, or at
least know the technical term for this so I can do my own searching?

There are already many sites that do exactly this bit.ly, j.mp,
tinyurl.com, etc.

Yes, these sites do this for the express purpose of shortening the URL,
but what you’re describing is really the same thing. Do a Google search
for URL shortening scripts and see how they work. That should get you
started.

It looks like you may not have seen the beginning of this post, let me
just repost what may not have showed on ruby-forum.com:

"Is it possible to make a site that only preforms a redirect to
another
site? Here’s an example:

Let’s just say that a netflix.com URL to a movie is something like
Netflix"

I kinda thought about that, but I think one of the main parts of the
site I’m trying to do is that it converts and substitutes things from
my site to something on the site I’m redirecting to. For example:

In netflixit.com/fantasy/heroes
netflixit.com converts to netflix.com/search?
/fantasy/ converts to category=Fantasy
/heroes/ converts to &name=Heroes

So what I need it to do is both redirect and transform some of the
stuff. There will only and always be 2 sections, such /category/
title/, so that may make it easier. If a user doesn’t put the correct
amount of sections, I’ll just take them to the home page
netflixit.com, or whatever.

Okay, I’ve been doing some looking around and it looks like using the
redirect helper is what I need. Does this look it would work?

resources :movies
match “/:category/:movie” => redirect(“Netflix
{category}&name=%{movie}”)

I don’t exactly know what document to put this kind of code in though,
or in what context :(. The only point of the site would be to preform
that code though, like the site is extremely plain and basic. I don’t
need statistics or anything (yet). A counter for the amount of
redirects would be nice though. How would I make a very basic app that
is just able to complete this code? There’s a tiny part covering this
on railscasts.com, but he doesn’t show making the actual app, just
using this little piece within it.

Thanks for taking the time to reply :slight_smile:

On 12/02/2010 04:00 PM, MOLTEN wrote:

Okay, I’ve been doing some looking around and it looks like using the
redirect helper is what I need. Does this look it would work?

resources :movies
match “/:category/:movie” => redirect(“Netflix
{category}&name=%{movie}”)

Why do you want to realize this with rails? As far as i can see, you can
realize what you want to do with apache’s mod_rewrite.

Cheers,

Jan

On Dec 2, 2010, at 5:15 PM, MOLTEN wrote:

then I get an error “bad URI(is not URI?):” since it tries to go to
the site with a space still in the URL.

How are these URLs getting to you at this point with spaces in them?
Where are people clicking on URLs that contain spaces? That’s where I
would start with this.

How do I substitute spaces
inside symbols with something like a “+”? It has to be before it gets
redirected though. I’d like to be able to just type like
netflixit.com/
dramatic comedy/heroes and it automatically turn the space into a “+”
right before it redirects, instead of manually typing the “+” in
netflixit.com/dramatic+comedy/heroes, which works correctly. So, I
guess before the URL a person types in gets to match
“/:category/:movie”, it has to replace spaces with “+”.

Are you doing this in your routes file? Or are you in a controller at
this point? Do you have access to the url_for helper somehow? I
suspect that could do this sort of escaping for you.

Or dig into that helper and see what it uses under the hood.

Walter

Well, it’s not a real website. People aren’t going to like go to it to
click on stuff. The whole purpose of it is to just get to a specific
point on another website quickly, if you already know where you’re
going. I know, it doesn’t sound that useful, but for what I’m using it
for, it is :D. I want people to be able to just type in like
netflixit.com/category/movie that they already know, and have it take
them straight to the netflix page for that movie. I guess it works
kind of like a URL shortener, but the URLs actually mean something. I
want it to be natural for people, so if some movie naturally has a
space in the title, they can just type like “netflixit.com/category/30
rock” instead of having to know how netflix handles spaces in their
URLs.

On Dec 2, 2:29pm, Walter Lee D. [email protected] wrote:

redirected though. I’d like to be able to just type like

Or dig into that helper and see what it uses under the hood.

Walter

So far I’m only working in the routes file. I’m really new to Rails,
but I hope to expand on this idea in the future. Do I need to start
doing something in another file? I would assume I have access to
url_for helper, I’m on my local machine.

Is there a way to do this just with variables instead of symbols, so I
could at least change them?

MOLTEN wrote in post #965822:

match “/:category/:movie” => redirect(“http://netflix.com/search?
category=%{category}&movie=%{movie}”)

The only problems so far are if a category or movie has a space in it,
then I get an error “bad URI(is not URI?):” since it tries to go to
the site with a space still in the URL. How do I substitute spaces
inside symbols with something like a “+”? It has to be before it gets
redirected though. I’d like to be able to just type like netflixit.com/
dramatic comedy/heroes and it automatically turn the space into a “+”
right before it redirects, instead of manually typing the “+” in
netflixit.com/dramatic+comedy/heroes, which works correctly. So, I
guess before the URL a person types in gets to match
“/:category/:movie”, it has to replace spaces with “+”.

If you want to do anything useful with this route then you need to
create a controller:

routes.rb

match “/:category/:movie” => “redirector#search”

redirector_controller.rb

def search

Build your URL

url =
CGI::escape(“netflix.com/search?category=#{params[:category]}&name=#{params[:movie]}&blahtechnical-stuff”)

Do anything else you want before redirecting

Redirect to the constructed URL

redirect_to(url)
end

Note: This may be a horrible implementation and may not be suitable for
production. Just here to illustrate what sorts of things you can do once
you get into an actual controller rather than trying to rely solely on
the routes file.

P.S. As aluded to earlier Rails may not be the best framework for what
you’re trying to do unless there’s much more that the application will
need to do. A Sinatra or simple Rack application may be all you need and
would be considerably more efficient for this purpose.

Okay, I took your guys’ advice and found a pretty good Sinatra
tutorial for making a URL shortener. I’ve almost completed it, but I’m
still getting some weird errors since for some reason [""] is being
appended to the URLs. If anyone has a chance, you can look at
http://railsforum.com/viewtopic.php?id=41958 to see my code and errors
for the two files (they’re short).

Thanks

I’m doing it with Rails since I’d eventually like to gather and
display information such as how many movies were redirected and what
categories or whatever were the most popular.

Anyway, I’ve got it working with:

match “/:category/:movie” => redirect(“http://netflix.com/search?
category=%{category}&movie=%{movie}”)

The only problems so far are if a category or movie has a space in it,
then I get an error “bad URI(is not URI?):” since it tries to go to
the site with a space still in the URL. How do I substitute spaces
inside symbols with something like a “+”? It has to be before it gets
redirected though. I’d like to be able to just type like netflixit.com/
dramatic comedy/heroes and it automatically turn the space into a “+”
right before it redirects, instead of manually typing the “+” in
netflixit.com/dramatic+comedy/heroes, which works correctly. So, I
guess before the URL a person types in gets to match
“/:category/:movie”, it has to replace spaces with “+”.

Thanks