# Routes question \[Catching URL in a variable\]

**URL:** <https://www.ruby-forum.com/t/routes-question-catching-url-in-a-variable/67875>\
**Category:** Rails\
**Created:** [February 14, 2006, 10:14am UTC](https://www.ruby-forum.com/t/routes-question-catching-url-in-a-variable/67875 "2006-02-14T10:14:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 14, 2006, 10:14am UTC](https://www.ruby-forum.com/t/routes-question-catching-url-in-a-variable/67875/1 "2006-02-14T10:14:37Z")

</div>

Hello Rails Gurus,

I have spent the entire night trying to figure this out with no luck.  
I will really appreciate if someone can come to my rescue.

I have a URL like the following:

[http://domain.com/resource/http://www.rubyonrails.org](http://domain.com/resource/http://www.rubyonrails.org)

and would like to capture the rubyonrails URL after the  
[http://domain.com/resource/](http://domain.com/resource/) part in a variable.

I tried something like

map.connect ‘resource/:protocol//:link’ …

but since I cannot accurately predict the number of forward slashes  
involved, I cannot hard code the route.

Is there any way I can tell in routes.rb to capture everything after  
“resource/” part and store it in a variable?

I highly appreciate your assistance.

Frank

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 14, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/routes-question-catching-url-in-a-variable/67875/2 "2006-02-14T12:55:49Z")

</div>

Possibly unrelated, but that URLs probably not legal. The secondary  
url needs to be encoded.

A.

softwareengineer 99 wrote:

> Hello Rails Gurus,
> 
> I have spent the entire night trying to figure this out with no luck.  
> I will really appreciate if someone can come to my rescue.
> 
> I have a URL like the following:
> 
> [http://domain.com/resource/http://www.rubyonrails.org](http://domain.com/resource/http://www.rubyonrails.org)
> 
> and would like to capture the rubyonrails URL after the  
> [http://domain.com/resource/](http://domain.com/resource/) part in a variable.
> 
> I tried something like
> 
> map.connect ‘resource/:protocol//:link’ …
> 
> but since I cannot accurately predict the number of forward slashes  
> involved, I cannot hard code the route.
> 
> Is there any way I can tell in routes.rb to capture everything after  
> “resource/” part and store it in a variable?
> 
> I highly appreciate your assistance.
> 
> Frank

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 14, 2006, 12:58pm UTC](https://www.ruby-forum.com/t/routes-question-catching-url-in-a-variable/67875/3 "2006-02-14T12:58:36Z")

</div>

Oops, early send.

[http://domain.com/resource/http%3A%2F%2Fwww.rubyonrails.org](http://domain.com/resource/http%3A%2F%2Fwww.rubyonrails.org)

It could be that by escaping the reserved characters, your life gets a  
bit easier in the routing.

A.

Alan F. wrote:

> Possibly unrelated, but that URLs probably not legal. The secondary  
> url needs to be encoded.
> 
> A.

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 14, 2006, 5:13pm UTC](https://www.ruby-forum.com/t/routes-question-catching-url-in-a-variable/67875/4 "2006-02-14T17:13:08Z")

</div>

On Feb 14, 2006, at 3:58 AM, Alan F. wrote:

> > Possibly unrelated, but that URLs probably not legal. The secondary  
> > url needs to be encoded.

```
Frank-

You can also just use a catch all route to handle this:

```

map.connect ‘/resource/\*url’, :controller =\> ‘foo’, :action =\> ‘bar’

```
Make sure to put that route at the bottom of your routes file so it

```

doesn’t match other urls on accident.

Cheers-  
-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 14, 2006, 9:50pm UTC](https://www.ruby-forum.com/t/routes-question-catching-url-in-a-variable/67875/5 "2006-02-14T21:50:23Z")

</div>

Thanks for the reply. The URL is legal, see:  
[http://technorati.com/search/http://www.google.com](http://technorati.com/search/http://www.google.com)

The funny thing is when I encode the URL, Apache doesn’t pass the  
request to dispatcher.fcgi. For unencoded URLs the request gets passed  
perfectly and the only issue is how to determine the number of slashed  
(nearly impossible).

So it means we cannot tell Rails via routes.rb that to catch  
everything after a certain string in a variable, unles its encoded.

Thank you very much for your replies.

Frank

Alan F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote: Possibly unrelated, but  
that URLs probably not legal. The secondary  
url needs to be encoded.

A.

softwareengineer 99 wrote:

> [http://domain.com/resource/](http://domain.com/resource/) part in a variable.
> 
> I highly appreciate your assistance.
> 
> Frank

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 14, 2006, 10:02pm UTC](https://www.ruby-forum.com/t/routes-question-catching-url-in-a-variable/67875/6 "2006-02-14T22:02:30Z")

</div>

On Feb 14, 2006, at 2:59 PM, Jason P. wrote:

> I think he meant, "check the 1738,

Should read, “check the RFC”

–  
Jason P.

---

<div class="post-metadata">

**Author:** ![softwareengineer\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b782af/32.png) [@softwareengineer\_99](https://www.ruby-forum.com/u/softwareengineer_99)\
**Post date:** [February 14, 2006, 9:59pm UTC](https://www.ruby-forum.com/t/routes-question-catching-url-in-a-variable/67875/7 "2006-02-14T21:59:28Z")

</div>

On Feb 14, 2006, at 2:49 PM, softwareengineer 99 wrote:

> Thanks for the reply. The URL is legal, see:  
> [http://technorati.com/search/http://www.google.com](http://technorati.com/search/http://www.google.com)

I think he meant, “check the 1738,” instead of, “see if, as an edge  
case, you can get this to work on any websites.” And RFC 1738 reads:

```
Many URL schemes reserve certain characters for a special meaning:
their appearance in the scheme-specific part of the URL has a
designated semantics. If the character corresponding to an octet is
reserved in a scheme, the octet must be encoded. The characters

```

“;”,  
“/”, “?”, “:”, “@”, “=” and “&” are the characters which may be  
reserved for special meaning within a scheme. No other characters  
may  
be reserved within a scheme.

```
Usually a URL has the same interpretation when an octet is
represented by a character and when it encoded. However, this is not
true for reserved characters: encoding a character reserved for a
particular scheme may change the semantics of a URL.

Thus, only alphanumerics, the special characters "$-_.+!*'(),", and
reserved characters used for their reserved purposes may be used
unencoded within a URL.

On the other hand, characters that are not required to be encoded
(including alphanumerics) may be encoded within the scheme-specific
part of a URL, as long as they are not being used for a reserved
purpose.

```

The third paragraph is relevant to what you’re suggesting.

–  
Jason P.
