Rails Subdomain on Localhost

rails subdomain on localhost

After saving /etc/hosts file, run your rails app like this

rails s -p 3000 -b daycare.no

In a browser

 http://daycare.no:3000

Personally, I use lvh.me:3000 just running

rails s -p 3000 -b lvh.me

no need to touch /etc/hosts file.

By default, you can't browse the link lvh.me:3000 without internet connection, solution is to add 127.0.0.1 lvh.me into host file.

# /etc/hosts file
127.0.0.1 localhost
127.0.0.1 lvh.me #make sure lvh.me is after localhost otherwise will not work

But, Its little annoying to run this each time when restarting a server.

Set your custom command:

sudo nano .bash_profile
# OR
sudo nano .bashrc
# OR
sudo nano .profile

And ADD these lines to there:

alias lvh='rails s -p 3000 -b lvh.me'
alias lvh_production='RAILS_ENV=production rails s -p 3000 -b lvh.me' #production

Do not forget restart your terminal tab, close and open new tab OR run this command on the same tab . ~/.bash_profile depends what you have used on top.


Alternate solution is POW (LINK) server can give you custom domain smth like daycare.dev.

How to map a subdomain to an existing controller action in localhost

Your example and the examples in most other answers are actually not using subdomains at all: If you look at the domain name store.dev then it actually has no subdomain, store.dev is a top-level/root domain. An example for a subdomain would be: sub.store.dev

If you change your entry in the /etc/hosts file from store.dev to store.local.dev then your code will work.

Update

To answer the second part of the question (restricting a route to only the root domain): you can achieve this by wrapping the routes that should only be available on the root domain in another constraints block with an empty subdomain:

constraints subdomain: '' do
get '/about'
end

Adding Subdomain for my ruby on rails server, how to do?

You can use the lvh.me domain. That domain has a DNS entry that will redirect to localhost. This also works for subdomains, so you can visit admin.lvh.me:3000 and it will redirect to localhost:3000 while still having the subdomain available in the Rails request.

The advantage is that you don't have to edit your localhost file.

Subdomains and locally installed Rails app

you might just want to try putting the actual dot com in your /etc/hosts file.

ie:


127.0.0.1 sub1.myapp.com
127.0.0.1 myapp.com
127.0.0.1 anyothersubdomains.myapp.com

what this usually does is trick your computer into thinking it is the host of all of those, so you can't go to the real site anymore in a web browser.

if you do want it to be .local, presumably so that you can refer to the real online site while working on a local copy, you should probably take a look in app/controllers/application_controller.rb (sometimes application.rb) and look for logic in there that helps determine what to do depending on the subdomain. maybe its hard coded to only look for a .com or something.

rails subdomain

You have to setup your DNS to point to your webserver. Just telling Rails to answer for a specific domain doesn't setup the DNS to do the same.

$ nslookup kingpangilinan.ngtv2.info
** server can't find kingpangilinan.ngtv2.info: NXDOMAIN

Use the interface of your domain provider or ask them directly to setup a wildcard DNS entry.

If you are using Rails 3 you shouldn't be using that Railscast (it's from 2008 anyway). There's a newer version which is specifically targeted for Rails 3: http://railscasts.com/episodes/221-subdomains-in-rails-3

By the way: You should not deploy a yet unfinished app without any protection. Development should happen locally or at least behind a password secured website.

Spree Rails multi store subdomains does not work on live server

Server name missing from your apache config for subdomain:

<VirtualHost *:80>
ServerName store1.mydomain.com
# Tell your app's 'public' directory path
DocumentRoot /var/www/store1.mydomain.com/public
</VirtualHost>

For dynamic or n subdomains:

<VirtualHost *:80>
ServerName mydomain.com
ServerAlias *.mydomain.com
# Tell your app's 'public' directory path
DocumentRoot /var/www/mydomain.com/public
</VirtualHost>


Related Topics



Leave a reply



Submit