How to prevent webserver renderening default html page(like index.htm etc) if found in a directory

StackOverflow https://stackoverflow.com/questions/22321568

  •  12-06-2023
  •  | 
  •  

Question

I wrote a ruby program which creates a webserver on 2000 port which is used for displaying directory structure present in linux node when accessed through browser using just HTTP URL.

require 'webrick'
include WEBrick

s = HTTPServer.new(
:Port            => 2000,
:DocumentRoot    => "/home/abc"
)
s.start()

I'm accessing throgh: http://10.x.x.x:2000/dir1/dir2...

Here i'm facing problem when "index.html or index.htm or index.jsp" present in any particular directory. Webserver just rendering that page instead of showing directory/file structure present in the directory.

So how to prevent webserver rendering default html page(like index.htm etc) if foun d in a directory instead it should show dir/file structure of that directory. How can i achieve this?

Was it helpful?

Solution

There's a :DirectoryIndex configuration for the WEBrick http server. However, the WEBrick project is poor documented.

You can set that option to [] to disable the directory index page look up.

require 'webrick'
include WEBrick

s = HTTPServer.new(
  :Port            => 2000,
  :DocumentRoot    => "/home/abc",
  :DirectoryIndex  => []
)
s.start()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top