How do I route by domain / subdomain in rails - ruby-on-rails

I looked at subdomain-fu and it looks pretty easy to route all non-www and non-'' subdomain requests to a single controller.
But I also, need to send all external domains that are CNAME'd to my domain to the same controller. I have done a lot of searching and I can't find anything.
Summarized, if it is a subdomain on my domain it goes to Catchall controller, if it is any other domain than my domain, it goes to the same Catchall controller.
I am going nuts on this, any help would be appreciated.

You should check out the request_routing plugin. It allows you to easily route request by: subdomain, domain, method, port, remote_ip, content_type, protocol, etc.

Related

Ember 2 Routing Subdomains

I have a Ember 2 application (ember-cli) that uses a Rails API as the back end. For this application, I have enabled Wildcard DNS with my DNS Provider (Cloudflare). When a user signs up with my website, I want them to be able to use their subdomain to access their public home page.
For example:
A user named Steve signs up for my site located at awesome.com. So Steve browses to steve.awesome.com, which internally would translate to awesome.com/users/steve. How do I setup my Ember routes such that it can route based off of the subdomain?
I have come to a solution, but it isn't exactly what I was initially looking for. I realized there really isn't a reason why the URL has to be awesome.com/users/steve, and instead have decided that their subdomain (or custom domain) will act as their identifier. So let's say Steve browses to steve.awesome.com, I will figure out the host via window.location.hostname, and use that as a lookup key to pass to my Rails API and retrieve user data.
Not exactly the solution I originally was seeking, but it solves my issue!

Rails routing to subdomain

We have a site at www.example.com but needs to move it to subdomain1.example.com, while maintaining the SEO links. We are planning to use Google's Change of Address but it requires 301 redirects.
We plan to release another site at the www.example.com address.
Is there a way to changes the routes.rb of www.example.com so that certain routes gets redirected to subdomain1 and others don't?
I am not sure exactly what you mean by redirect but here are some options:
You can redirect a route to another route in your rails app using:
get '/some_route', to: redirect('/other_route')
Or if the subdomain is a seperate app / service you can redirect in a controller using:
redirect_to "http://www.rubyonrails.org"

Subdomain URL Rediretion In MVC

I am new in MVC, I have a list of url redirection:
•website1.domain.com goes to domain.com\websites\1
•website2.domain.com goes to domain.com\websites\2
This is a dynamic mapping like this: websiteN.domain.com goes to domain.com\websites\N
How can I do this in MVC, Do I need to use routing? or I need only URL redirection?
This is a duplicate question.
Everything you need can be done in IIS.
please visit this Stack link:
handling sub-domains in IIS for a web application
(same user asked this question and reposted How can we make an ASP.NET MVC4 route based on a subdomain?)
you can get more detailed information:
http://www.dotnetexpertguide.com/2012/04/aspnet-iis-dns-records-sub-domain-on.html
http://content.websitegear.com/article/subdomain_setup.htm
I've had a similar situation where I needed to make sure that the language code was in the url.
My solution was to write an http module. You'll want this module to inspect the request and see what subdomain the request is under. If it is a subdomain, then you'll want to redirect them to the correct directory under domain.com

Redirecting subdomains with the Rails router

We have an application which uses five subdomains. One of those subdomains is changing.
I would like to be able to redirect everyone hitting the old domain to be redirected to the new one, and ideally use the Rails router so that I leave the processing there using Rack.
Is there a way of redirecting all traffic to one domain, to be redirected to another.
e.g all traffic to foo.app.com get's redirected to bar.foo.com, whilst maintaining the full path & query string.
As I am hosting with Heroku I have no .htaccess.
Refraction may be able to help with that.

Multi-level Subdomains in Rails

Can we have multi-level subdomain in Rails like this?
sub1.sub2.mysite.com
Yes, you can check the subdomain the app is accessed from using request.subdomains
But if you are going to do something more advanced you should probably use
subdomain-fu
I don't see any reason why you couldn't have a subdomain of a subdomain being used in a rails app.
You'll want to setup a wildcard A record in your domain DNS and configure your http server to accept wildcard server aliases.
Then in your application_controller you'll probably want a before_filter that does some juju with
request.host
to do whatever you want, be it
Account.find_by_domain(parsed_request_host) or whatever.

Resources