Welcome Guest | Login

ActionController::AbstractRequest.relative_url_root

  I've noticed on multiple occasions some confusion about this relative_url_root command, where to put it, and when/why to use it.  Hopefully this will clear things up a bit.  

When to use it

If you are running your app completely in a subfolder of your domain.

For example, if you want to run your blog as www.domain.com/blog and have it be a totally different Rails app than www.domain.com.

Also note in this scenario you would need the below code in your www.domain.com/.htaccess just under where it says 'use the following rewrite rules so that Apache won't rewrite certain requests':
RewriteCond %{REQUEST_URI} ^/blog.*
RewriteRule .* - [L]
and in your environment.rb (see below where to put it) you'll need the good ol':
ActionController::AbstractRequest.relative_url_root = "/blog"
other than this sort of www.domain.com/blog scenario, you shouldn't use it.

Why to use it

Essentially, it tells Rails to tack on an extra '/blog' to every URL it writes.  Routes in Rails are awesome, BTW. This way, it will know to sent requests into your 'blog' folder instead of into your main app.

Where to put it

You'll want to put it at the VERY bottom of your environment.rb file after the:
# Include your application configuration below
If you put it anywhere else, your app will likely run into routing troubles.



So hopefully that clears things up a bit.  Your app will load if the relative_url_root is there and it shouldn't be, but you won't be able to browse around to different controllers.   So if (for example) your images and CSS files don't load into your app correctly, Rails is telling the app to look in something like www.domain.com/blog/images/dude.jpg rather than www.domain.com/images/dude.jpg

Feel free to post below anytime if you have questions or comments.

Cheers,

~William

2006-09-01 04:25 PM

Thanks for the informative discussion of relative_url_root and how to set it in the environment.rb file.

This discussion seems focused on cases where www.domain.com/ is already hosting a rails app and you want a second app (app2) is to be placed in the subdomain app2.domain.com/ or the subfolder www.domain.com/app2.  However, I wonder how the instructions would change if the root domain is NOT hosting a rails app but instead static pages.  Would it then be necessary to add rewrite rules in ~/public_html/.htaccess or not?

And would it still be necessary (I assume yes) to add the relative_url_root instruction in the ~/app2/public/environment.rb file?

2007-11-20 03:12 PM

If the main domain is not rails and the sub domain is rails then accessing the main domain (static pages)wont be a problem. Also the sub-domain directory if sym-linked properly will defenitely work fine.

There is no need for such rewrite rules under such a situation.

2007-11-20 03:24 PM

Regards,
Rahul

Hello Guest! In order to post you must be an active client with us, please log in or sign up today!