This gem helps out when your application depends on subdomain support and you don't want to modify you /etc/hosts
file all the time for your development
environment.
- Add the gem to your
Gemfile
gem 'local-subdomain'
- Run
bundle install
- Include the
LocalSubdomain
module into yourapplication_controller.rb
(or the controllers that requires subdomain support)
class ApplicationController < ActionController::Base
include LocalSubdomain
....
end
NOTE: Do not force the gem only to be included in the development
group. Because of the inclusion of the module LocalSubdomain
, you'll need to have the gem available in every environment.
The gem itself contains guards to only perform changes when the environment is development
, so no worries or check it out yourself:
By default the gem uses the domain lvh.me
with the port used, when running the rails server, but it is also possible to provide a custom redirect domain and port using the following ENV
variables:
ENV | Notes | EXAMPLE |
---|---|---|
SERVER_REDIRECT_PORT |
The port number to redirect to | 5000 |
SERVER_REDIRECT_DOMAIN |
The domain to redirect to | my.domain.tld |
Basically it does two things:
- Extends the
Rack::Handler
to make sure we bind to0.0.0.0
instead oflocalhost
- Adds the
LocalSubdomain
module which executes abefore_action
to redirect tohttp://lvh.me:<port>
(or the configured redirect domain and port)
By default, this gem uses the domain http://lvh.me to handle our requests for our subdomain(s). Request to the domain lvh.me
redirects all requests to 127.0.0.1
.
This give's us the ability to browse to http://subsub.lvh.me:3000 and be handle request.subdomain
from our controllers.
Because we're going to use the external domain http://lvh.me which redirects to 127.0.0.1
we have to make our server not to bind to localhost
only.
This module includes a before_action
which will check if the request is served by http://lvh.me. If not it will redirect to the domain.
So when we browse to http://localhost:3000 it will redirect you to http://lvh.me:3000
I've tested the gem with: