# Set base url?

**URL:** <https://www.ruby-forum.com/t/set-base-url/77332>\
**Category:** Rails\
**Created:** [March 6, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/set-base-url/77332 "2006-03-06T17:08:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [March 6, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/set-base-url/77332/1 "2006-03-06T17:08:47Z")

</div>

I have an application running on a Lighttpd instance which is proxied by  
an  
Apache server. It seems to work fine but my urls are incorrect: all urls  
reference / which is not where my application runs at the Apache server.  
How do I set the base url of a Rails application?

My Apache has the following proxy rules:  
ProxyPass /hieraki [http://localhost:3001](http://localhost:3001)  
ProxyPassReverse /hieraki [http://localhost:3001](http://localhost:3001)  
But links in the Hieraki main page are like  
[http://someserver.com/xml/rss](http://someserver.com/xml/rss) for the RSS feed  
and [http://someserver.com/wiki/folder/show/ROOT](http://someserver.com/wiki/folder/show/ROOT) for the root.

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [March 6, 2006, 5:39pm UTC](https://www.ruby-forum.com/t/set-base-url/77332/2 "2006-03-06T17:39:15Z")

</div>

On Mon, Mar 06, 2006 at 05:03:52PM +0100, Bart B. wrote:  
} I have an application running on a Lighttpd instance which is proxied  
by an  
} Apache server. It seems to work fine but my urls are incorrect: all  
urls  
} reference / which is not where my application runs at the Apache  
server.  
} How do I set the base url of a Rails application?  
}  
} My Apache has the following proxy rules:  
} ProxyPass /hieraki [http://localhost:3001](http://localhost:3001)  
} ProxyPassReverse /hieraki [http://localhost:3001](http://localhost:3001)  
} But links in the Hieraki main page are like  
} [http://someserver.com/xml/rss](http://someserver.com/xml/rss) for the RSS feed  
} and [http://someserver.com/wiki/folder/show/ROOT](http://someserver.com/wiki/folder/show/ROOT) for the root.

I just answered another question with similar information. See  
[http://www.ruby-forum.com/topic/56927#43695](http://www.ruby-forum.com/topic/56927#43695)

–Greg

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [March 6, 2006, 10:29pm UTC](https://www.ruby-forum.com/t/set-base-url/77332/3 "2006-03-06T22:29:30Z")

</div>

On Mar 6, 2006, at 11:38 AM, Gregory S. wrote:

> I just answered another question with similar information. See  
> [Apache 2.x: Use fcgid or Mongrel? - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/56927#43695)

So you did… the correct answer is to use Rails’ relative\_url\_root.  
Although a config option for this setting is currently MIA, you can  
set it in your environment by doing

ActionController::AbstractRequest.relative\_url\_root = ‘/the/prefix/  
rails/should/add/to/generated/links/and/remove/from/incoming/paths’

Cheers,

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [March 7, 2006, 9:20am UTC](https://www.ruby-forum.com/t/set-base-url/77332/4 "2006-03-07T09:20:12Z")

</div>

Nicholas S. wrote:

> rails/should/add/to/generated/links/and/remove/from/incoming/paths’

I tested this solution today and it does not work. I tried it on hieraki  
and  
I get urls like [www.someserver.com/myurl/wiki/folder/show](http://www.someserver.com/myurl/wiki/folder/show) and when I  
click  
those I get  
Recognition failed for “der/show/ROOT”  
So the stripping does not seem to work?

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [March 7, 2006, 9:53am UTC](https://www.ruby-forum.com/t/set-base-url/77332/5 "2006-03-07T09:53:29Z")

</div>

Bart B. wrote:

> > ActionController::AbstractRequest.relative\_url\_root = ‘/the/prefix/  
> > rails/should/add/to/generated/links/and/remove/from/incoming/paths’
> 
> I tested this solution today and it does not work. I tried it on hieraki  
> and I get urls like [www.someserver.com/myurl/wiki/folder/show](http://www.someserver.com/myurl/wiki/folder/show) and when I  
> click those I get  
> Recognition failed for “der/show/ROOT”  
> So the stripping does not seem to work?

Update: I discovered the problem (but no solution). I am running Apache  
as a  
proxy for a local Rails setup. So I proxy [www.someserver.com/myurl](http://www.someserver.com/myurl) to  
localhost:3000 and I ask Rails to append myurl.  
But Rails also strips myurl when receiving a request, even worse: it  
only  
strips the number of characters in myurl. In my case it was thiswiki so  
8  
characters, so [www.someserver.com/myurl/wiki/folder/show/ROOT](http://www.someserver.com/myurl/wiki/folder/show/ROOT) has  
[www.someserver.com](http://www.someserver.com) stripped off which results in  
myurl/wiki/folder/show/ROOT and then 8 characters are stripped off:  
der/show/ROOT.  
How should this be fixed? Did I do something wrong with the url\_for  
method  
and should that work? Is this method completely wrong? I can’t be the  
only  
one in this situation, right?

Thanks for your help  
Bart

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [March 7, 2006, 6:03am UTC](https://www.ruby-forum.com/t/set-base-url/77332/6 "2006-03-07T06:03:52Z")

</div>

Gregory S. wrote:

> } [http://someserver.com/xml/rss](http://someserver.com/xml/rss) for the RSS feed  
> } and [http://someserver.com/wiki/folder/show/ROOT](http://someserver.com/wiki/folder/show/ROOT) for the root.
> 
> I just answered another question with similar information. See  
> [Apache 2.x: Use fcgid or Mongrel? - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/56927#43695)

Seems like a good solution to me, thanks! I wonder if there are non-hack  
solutions, but this one works fine for me.

Bart

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [March 7, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/set-base-url/77332/7 "2006-03-07T14:06:15Z")

</div>

On Tue, Mar 07, 2006 at 09:50:12AM +0100, Bart B. wrote:  
} Bart B. wrote:  
}  
} \> Nicholas S. wrote:  
} \>  
} \>\> On Mar 6, 2006, at 11:38 AM, Gregory S. wrote:  
} \>\>  
} \>\>\> I just answered another question with similar information. See  
} \>\>\> [Apache 2.x: Use fcgid or Mongrel? - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/56927#43695)  
} \>\>  
} \>\> So you did… the correct answer is to use Rails’  
relative\_url\_root.  
} \>\> Although a config option for this setting is currently MIA, you can  
} \>\> set it in your environment by doing  
} \>\>  
} \>\> ActionController::AbstractRequest.relative\_url\_root = ‘/the/prefix/  
} \>\> rails/should/add/to/generated/links/and/remove/from/incoming/paths’  
} \>\>  
} \> I tested this solution today and it does not work. I tried it on  
hieraki  
} \> and I get urls like [www.someserver.com/myurl/wiki/folder/show](http://www.someserver.com/myurl/wiki/folder/show) and  
when I  
} \> click those I get  
} \> Recognition failed for “der/show/ROOT”  
} \> So the stripping does not seem to work?  
}  
} Update: I discovered the problem (but no solution). I am running  
Apache as a  
} proxy for a local Rails setup. So I proxy [www.someserver.com/myurl](http://www.someserver.com/myurl) to  
} localhost:3000 and I ask Rails to append myurl.  
} But Rails also strips myurl when receiving a request, even worse: it  
only  
} strips the number of characters in myurl. In my case it was thiswiki  
so 8  
} characters, so [www.someserver.com/myurl/wiki/folder/show/ROOT](http://www.someserver.com/myurl/wiki/folder/show/ROOT) has  
} [www.someserver.com](http://www.someserver.com) stripped off which results in  
} myurl/wiki/folder/show/ROOT and then 8 characters are stripped off:  
} der/show/ROOT.  
} How should this be fixed? Did I do something wrong with the url\_for  
method  
} and should that work? Is this method completely wrong? I can’t be the  
only  
} one in this situation, right?

So now that I know the right way to do things, I can talk about how to  
make  
it work. Basically, you can’t use:

ProxyPass /foo/bar [http://localhost:3000/](http://localhost:3000/)  
ProxyPassReverse /foo/bar [http://localhost:3000/](http://localhost:3000/)

…because it strips too much. Instead, you use:

ProxyPass /foo/bar [http://localhost:3000/foo/bar](http://localhost:3000/foo/bar)  
ProxyPassReverse /foo/bar [http://localhost:3000/foo/bar](http://localhost:3000/foo/bar)

Now Rails will strip and add exactly what it should.

} Thanks for your help  
} Bart  
–Greg

---

<div class="post-metadata">

**Author:** ![bartb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@bartb](https://www.ruby-forum.com/u/bartb)\
**Post date:** [March 7, 2006, 4:08pm UTC](https://www.ruby-forum.com/t/set-base-url/77332/8 "2006-03-07T16:08:51Z")

</div>

Gregory S. wrote:

> Now Rails will strip and add exactly what it should.

When using this with  
ActionController::AbstractRequest.relative\_url\_root =’/foo/bar’  
I can’t get my regular toy example to work because I get  
Recognition failed for “”  
With hieraki routing seems to work (why?) but the CSS and javascript  
fails.  
When surfing to the CSS or Javascripts I get messages like:  
Recognition failed for “/stylesheets/base.css”  
Recognition failed for “/javascripts/effects.js”  
What is going on here? Is the routing not working in Rails? Or is this  
use  
of Apache as a proxy so uncommon?

Bart
