Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1091333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:30:38+00:00 2026-05-16T23:30:38+00:00

I wondered if there were any plugins or methods which allow me to convert

  • 0

I wondered if there were any plugins or methods which allow me to convert resource routes which allow me to place the controller name as a subdomain.

Examples:

map.resources :users
map.resource :account
map.resources :blog
...

example.com/users/mark
example.com/account
example.com/blog/subject
example.com/blog/subject/edit
...

#becomes

users.example.com/mark
account.example.com
blog.example.com/subject
blog.example.com/subject/edit
...

I realise I can do this with named routes but wondered if there were some way to keep my currently succinct routes.rb file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T23:30:39+00:00Added an answer on May 16, 2026 at 11:30 pm

    The best way to do it is to write a simple rack middleware library that rewrites the request headers so that your rails app gets the url you expect but from the user’s point of view the url doesn’t change. This way you don’t have to make any changes to your rails app (or the routes file)

    For example the rack lib would rewrite: users.example.com => example.com/users

    This gem should do exactly that for you: http://github.com/jtrupiano/rack-rewrite

    UPDATED WITH CODE EXAMPLE

    Note: this is quickly written, totally untested, but should set you on the right path. Also, I haven’t checked out the rack-rewrite gem, which might make this even simpler

    # your rack middleware lib.  stick this in you lib dir
    class RewriteSubdomainToPath
    
      def initialize(app)
        @app = app
      end
    
      def call(env)
        original_host = env['SERVER_NAME']
        subdomain = get_subdomain(original_host)
        if subdomain
          new_host = get_domain(original_host)
          env['PATH_INFO'] = [subdomain, env['PATH_INFO']].join('/')
          env['HTTP_X_FORWARDED_HOST'] = [original_host, new_host].join(', ')
          logger.info("Reroute: mapped #{original_host} => #{new_host}") if defined?(Rails.logger)
        end
    
        @app.call(env)
    
      end
    
      def get_subdomain
        # code to find a subdomain.  simple regex is probably find, but you might need to handle 
        # different TLD lengths for example .co.uk
        # google this, there are lots of examples
    
      end
    
      def get_domain
        # get the domain without the subdomain. same comments as above
      end
    end
    
    # then in an initializer
    Rails.application.config.middleware.use RewriteSubdomainToPath
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I always wondered: Are there any hard facts which would indicate that either shorter
I need to implement an OpenID Provider in .Net and wondered....Is there's any OpenSource
I just wondered if there are any libraries in Java or any other language
I just wondered whether there is any good reason for or even an advantage
I've been doing a bit of research regarding action filters and wondered if there
I am wondering if there is any way to dynamically rotate an image or
Small pet peev. When creating new visual studio projects is there any way to
I wondered about the best way to implement a correct, flexible and fast Equals
I've always wondered this - why can't you declare variables after a case label
I have always wondered how people update a previous line in a command line.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.