rails 3.0 subdomains and formats - ruby-on-rails-3

I have 2 subdomains that point to one rails application. My rails application's controllers should serve both subdomains.
Lets call the subdomains www.mydomain.com and demo.mydomain.com. I want 2 to accomplish 3 things:
Make sure only some of my controllers serve www.mydomain.com and some serve both domains.
Make sure only a subset of my controllers' actions serve www.mydomain.com and some serve both domains.
Make sure users use only format :html for demo.mydomain.com (e.g. http://demo.mydomain.com/index.html) and use only formats :json/:xml for www.mydomain.com (e.g. http://www.mydomain.com/index.json).
What's the best way to accomplish both requests ?

Perhaps take a look at request routing based on subdomain
How do I route by domain / subdomain in rails
http://agilewebdevelopment.com/plugins/request_routing
where the request_routing plugin would allow you to define routing requirements that test methods/properties of the request object such as subdomain, domain, port. You can test them either against a value or with a Regexp (assuming the method returns a string).
If you're using rails 3 you could also check out subdomaining your rails site
http://railscasts.com/episodes/221-subdomains-in-rails-3
In order to get your controllers to respond differently based on subdomain, you can use a conditional based on
#host = request.host

Related

Redirect one domain to another on same server using Apache

I have two domains, example.se and example.com, which I'd both like to point to the same server and just presenting different language translations when viewing the site. I'm running Apache and PHP and have set up a default page that takes a GET argument then sets the content to show in either language.
How would I configure this with Apache so that if the user types example.se, he/she would end up with example.com/?lang=se instead, while keeping the .se domain in the url? There's just one page on the site so no need to worry about routing.

Making Laravel understand proxy domain htaccess rule

I have a setup in Laravel whereby I have different sections of the site powered by one codebase. A section is defined by the first slug of the uri:
localhost.dev/section1/feature
localhost.dev/section2/another/feature
However, I also have a domain alias/proxy for these so that each section can have its own independent branding and SEO without splitting up the codebase.
section1.dev/feature (alias of localhost.dev/section1/feature)
section2.dev/another/feature (alias of localhost.dev/section2/another/feature)
However, Symfony's HTTPFoundation appears to be too smart to be fooled by this proxy, and whenever you use URL::full() or URL::current(), the domain remains localhost.dev despite your browser telling you that you're on section1.dev or section2.dev
Is there a way to configure .htaccess differently, or is there a way to make Laravel's URL::full() or URL::current() mirror what's in your address bar?
First: a domain alias is not a proxy.
Second: the app.url config option gets used by the application to generate URLs. Try making it an empty string.

API subdomain for Heroku app, is it possible?

I am trying to build an API and I am concerned that all my resources will either not be accessible with the api.myapp.com domain or that they will "live" with the wrong uris.
I have added the CNAME for my domain name to point to my Heroku app.
(ex: browsing to www.myapp.com takes you to https://myherokuapp.heroku.com)
I would like to set up an API subdomain, so that a GET to
https://api.myapp.com takes you to https://myherokuapp.heroku.com/api/v1
The best scenario would be that a POST to https://api.myapp.com/accounts/12345 would create a new account. Is that even possible?
(I know that subdomains (eg: mysubdomain.myappname.heroku.com) are not possible with Heroku)
I believe the answer could be in three different places:
Something to do with DNS provider forwarding configs (maybe
something to do with "A" records).
Something to config in Heroku, possibly a paid add-on to handle domains/subdomains.
Handle all subdomains within my app.
If you want to differentiate between api.mydomain.com and www.mydomain.com and have different controllers for your API requests then you could certainly use Rails routes constrained to your api subdomain to handle this
constraints :subdomain => "api" do
scope :module => "api", :as => "api" do
resources :posts
end
end
which would then use the posts_controller.rb in the app/controllers/api folder of your application.
You'll then have both www.mydomain.com and api.mydomain.com added a custom domains for your application and then the routes will take care of the rest.
You might also want to look into the Grape Gem for helping build your api

Redirecting from subdomain to query string

There is a list of options in my website home page - for example, http://example.com. When a user clicks on option1, I want to show the URL as http://option1.example.com and not http://example.com/xyz.php?opt=option1. Any help or guidance would be appreciated.
You will either need to create a subdomain for each option and then provide the required code at that subdomain. how you set that up varies by hosting provider.
Do you really need a seperate subdomain per option?
Could you use http://abc.ca/Option1/ or http://abc.ca/Options/1/ instead?
If not you will need to contact your hosting providers about creating subdomains option1.abc.ca etc and where to put your php pages.
If you are hosting your own server with apache, I think it is possible to use a * dns entry to accept all subdomains and then you can use $_SERVER variables to get the domain being requested.

Can I mix top-level domain with subdomain applications on Heroku using "Custom Domains" add-on?

According to heroku, http://addons.heroku.com/custom_domains
"The wildcard domain add-on enables you to use *.yourdomain.com. You can have as many wildcard domains as you like on one app."
Let say i have a domain call abc.com
can i use abc.com for 1st rails app, another.abc.com for 2nd rails app, another2.abc.com for 3nd rails app? this 3 rails is separate/different heroku rails app? i would like to know cause heroku say "as many wildcard domains as you like on one app."
Yes, this should be possible. You can condense your apps basically into 1 (if even necessary), and follow the guide here
http://www.railsdev.ws/blog/10/using-subdomains-in-rails-apps/
to learn how to route based on subdomains.
If you have 3 separate applications on Heroku as you describe then you just need a single custom domain per application (ie not the wildcard addon). If you use the wildcard domain addon to add say *.mydomain.com then you will not be able to use the same domain (mydomain.com) for any subsequent applications you host on Heroku.