Take the 2-minute tour ×

A site I manage is getting constant requests for a javascript file that no longer exists, from an older version of the site. These requests take up a lot of resources because they get routed through Rails every time to return a 404. I am thinking it would be much better to have Rack handle that specific URL and return 404 itself. Is that correct? If so, how would I set that up?

I have been checking out this blog post which I think is kinda the way to move forward (ie, inheritance from some existing Rack module):


share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

So I ended up writing my own little bit of middleware:

module Rack

  class NotFoundUrls

    def initialize(app, exclude)
      @app = app
      @exclude = exclude

    def call(env)

      status, headers, response = @app.call(env)

      req = Rack::Request.new(env)
      return [status, headers, response] if !@exclude.include?(URI.unescape(req.fullpath))

      content = 'Not Found'
      [404, {'Content-Type' => 'text/html', 'Content-Length' => content.size.to_s}, [content]]




and then adding this to the config.ru file:

use Rack::NotFoundUrls, ['/javascripts/some.old.file.js']

It's the first time I've done this so let me know if there's any glaring mistakes...

share|improve this answer
add comment

The rack-contrib gem includes a Rack::NotFound middleware component (among many other useful elements) which should do the job:


share|improve this answer
This looks promising, but it seems to just define a default 404 endpoint? How would I define that all requests for that one file should go to it too? –  koosa Oct 25 '13 at 17:13
add comment

Your Answer


