0

I have put "index.html" files into every folder to create my pages.

For example www.mysite.com/volunteers/jasmine/ will display jasmine's page which is the index.html inside the folder "jasmine". And the "volunteers" folder doesn't contain any "index.html" file but when it's typed in the url as follows mysite.com/volunteers/ it displays all the folders inside volunteers folder under the heading "Index of /volunteers"

404 error document helps only to redirect the mysite.com/volunteers not mysite.com/volunteers/ (which contains a slash in the end)

Please help me get rid of this situation.

Thank you in advance!

2 Answers2

1

in your main folder of www.mysite.com create a file called .htaccess

In that file add this line at the top and save it:

IndexIgnore * 

Assuming you are using Apache web server, if configured to check .htaccess (which most are, especially on shared hosts), it will read this rule and prevent indexes of your directories.

Remember the file is called .htaccess (with the dot at the beginning and no file suffix at end)


Here is something to play around with to give you ideas as well, where you test a rule if a file or direct (-f or -d) not found, then perform redirect to file of your choice:

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/volunteers/$1/$2 !-f
RewriteCond %{DOCUMENT_ROOT}/volunteers/$1/$2 !-d
RewriteRule ^volunteers/ /pageOfYourChoice.html? [R=301,L]

The server rules are at times trial/error which is why I shared that URL with .htacess tricks for you to determine what is best scenario for what you want to achieve. Here you might want to remove the $2 and just assume everything in volunteers but unsure how you set up your app and how many directories you go. It's trial/error and nice thing with .htaccess is no server reloads/restarts required so try it, test, repeat.

Mike S.
  • 4,806
  • 1
  • 33
  • 35
  • Thank you Mike! that worked but it still displays the heading "Index of volunteers" but not the folders anymore. Can I get rid of that page being displayed and redirect it to some other page. – Sarah Noah Jul 15 '12 at 12:38
  • Sarah you might play with this command instead of IndexIgnore * (remove that and try this): Options All -Indexes it gets tricky and several ways to do things. Another option is DirectoryIndex and you list filenames and if one not found it'll try another, etc. Here's a link that might help: http://perishablepress.com/stupid-htaccess-tricks/ – Mike S. Jul 15 '12 at 12:47
  • In that article above, look at the 301 redirect option. It may be too powerful for what you need so play around until your desired behavior is met. – Mike S. Jul 15 '12 at 12:50
  • Option All - Indexes overrides the ErrorDocument 404 /notfound.html and the DirectoryIndex doesn't make any change. – Sarah Noah Jul 15 '12 at 13:00
  • I edited my post with another option for you too, but it is trial/error as noted. – Mike S. Jul 15 '12 at 14:26
0

The information from the other answer about Apache and the .htaccess file is correct, but if you're using Microsoft IIS as your web server rather than Apache, you'll need to disable directory browsing. See the information here:

http://technet.microsoft.com/en-us/library/cc731109(v=ws.10)

Another thing to consider is that you could simply create an INDEX.HTML page for that folder which has a META REFRESH tag to push the user somewhere else.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <title>Untitled Document</title>
  </head>
  <meta http-equiv="refresh" content="0;URL=/index.html" />
  <body>
  </body>
</html>

You can do a redirect in the .htaccess file as well but for most users creating the HTML file is quicker and easier.

Mike Fulton
  • 920
  • 11
  • 22
  • Yeah, I could do that but it'd b a lot of work when I do for other folders (a lot) like the same. So is there any short method? and how can you do that using .htaccess Mike? pls? – Sarah Noah Jul 15 '12 at 12:47
  • You'd have to do the .htaccess for each folder also. – Mike Fulton Jul 15 '12 at 12:58
  • Oops.. I thought it could be done in the root folder :/ any other alternative? – Sarah Noah Jul 15 '12 at 13:17
  • Sorry, there's probably some way to do it with a single .htaccess file in the right place, but I dunno how. – Mike Fulton Jul 15 '12 at 13:36
  • You don't need .htaccess in every folder, they just override root if exist but not required. There are more complex things you can do if you enable mod rewrite, but not sure how much you want to dive into. – Mike S. Jul 15 '12 at 14:15