20

I have a rails app (Rails 3.0) that I need to temporarily take out of service. While this is in effect, I want to create a new route that will direct all requests to a single piece of static content. I have a controller set up to serve my static pages.

I tried something like this:

match '*' => 'content#holding'

and

match '*/*' => 'content#holding'

to match a wildcard route as described here:Rails 3 route globbing without success.

This is probably a really simple answer, but I couldn't figure it out.

/EDIT/ Forgot to mention that I did have this rule at the very top of my routes.rb file.

Nick
  • 949
  • 1
  • 9
  • 14

4 Answers4

42

Rails needs to bind the url parameters to a variable, try this:

match '*foo' => 'content#holding'

If you also want to match /, use parenthesis to specify that foo is optional:

match '(*foo)' => 'content#holding'
klochner
  • 7,901
  • 1
  • 31
  • 45
  • This seems to work for all paths, but not for the root itself. I added an additional temporary root route to the top of routes.rb as well, so now it is working how I want - thanks! – Nick Dec 30 '11 at 16:14
  • 1
    My end result looks like this: "match '*foo' => redirect('/') #NEWLINE root :to => 'content#holding'" Basically I took your idea, added the redirect, then routed root temporarily to my holding page. – Nick Dec 30 '11 at 16:32
  • 1
    if you're redirecting, make sure it's a 302 (moved temporarily) rather than a 301 (moved permanently) – klochner Dec 30 '11 at 16:45
  • It's working like a charm. But can you share in which place the official document show these information? – hiveer Dec 28 '18 at 09:08
  • I'd suggest using `all` as a more descriptive variable name. – Lerk Oct 21 '20 at 22:29
2

I did this just yesterday and first came up with the solution that klochner shows. What I didn't like about this is the fact that whatever you enter in the URL, stays there after the page loads, and since I wanted a catch all route that redirects to my root_url, that wasn't very appealing.

What I came up with looks like this:

# in routes.rb
get '*ignore_me' => 'site#unknown_url'

# in SiteController
def unknown_url
  redirect_to root_url
end

Remember to stick the routes entry at the very bottom of the file!

EDIT: As Nick pointed out, you can also do the redirect directly in the routes file.

cvshepherd
  • 3,717
  • 2
  • 17
  • 13
1

I ran into something like this where I had domain names as a parameter in my route:

match '/:domain_name/', :to => 'sitedetails#index', :domain_name => /.*/, :as =>'sitedetails'

The key piece to this was the /.*/ which was a wildcard for pretty much anything. So maybe you could do something like:

match '/:path/', :to => 'content#holding', :path=> /.*/, :as =>'whatever_you_want'
fregas
  • 3,112
  • 3
  • 23
  • 41
0

Where in "routes.rb" is this line located?

To have priority over other routes, it has to be placed first.

As an alternative, you can look into this: http://onehub.com/blog/posts/rails-maintenance-pages-done-right/

Or this: Rails: admin-only maintenance mode

Community
  • 1
  • 1
Sergio Tulentsev
  • 210,238
  • 40
  • 347
  • 343