hiding index directories? .htaccess?

They have: 3 posts

Joined: Jan 2005

i have many foders within my site that dont have index pages. is there a way of redirecting these to the 1st parent directory that has an index.htm

or at least to my homepage?

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I'm not sure if this can be completely done in the .htaccess or not.

Using both .htaccess and a script it can be done. Changing the .htaccess to block dislaying directories without indexes, then setting a custom 403 error that runs a script to check back up the directory tree for a proper index.

I'm in a rush right now. Later on if no one else posts a script for it (or another method) I'll write one to put on here.

-Greg

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

You could easily do it back to your homepage by setting the 404 error to redirect the user to the homepage, however this is not considered good 404 handling - what about a genuine 404, a dead link points the user back to the homepage - now that would be confusing! Possibly the best way is via script (unless you setup each .htaccess for every folder manually, this wouldn't be worth the time expense as you could have spent the time building indexes for every folder!)

We'll see what Greg comes back with Smiling

a Padded Cell our articles site!

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

Well, for directories without indexs, you'd set the htaccess to not list the files in the directories, by placing in the most parent directory a .htaccess of:

Options -indexes

then, you'd have to add a 403 error page, since they would be forbidden from that directory, and you could have it act as a redirect page, or something to that effect. By default, you can name the page Error403.html (I think) that will differ depending on your server, but I think there is .htaccess to set that as well.

Roo's picture

She has: 840 posts

Joined: Apr 1999

I redirected *to* an index page with .htacces for my Gallery...I wanted to over ride the index.php page so I used this in .htaccess:

DirectoryIndex index.html index.php

I'm not sure if it will work, but you could try:
DirectoryIndex yourpage.html index.html

Just make a copy of the file, so you can go back to it, if it doesn't work.

Roo

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

No, he wanted them redirected to the parent directory, that sadly wouldn't do it, unless you can do:

DirectoryIndex ../index.html

I haven't tried, but I have my doubts.

[EDIT]
I was wrong, you CAN do that, ../ (though it will depend on how many levels down you go) but /index.html will redirect to the parent. Only 1 problem with that, if images aren't listed from the base dir /images/whatever (leading slash most important) then they won't show, because the browser is confused. php or other server side applications don't care, because the same file is being executed, it's just something different being sent to the browser.

They have: 3 posts

Joined: Jan 2005

thanks so much guys for replying.

im not fussed if it goes to the parent directory, that would be ideal.

at least if it gets redirected to my homepage that would be great!

They have: 3 posts

Joined: Jan 2005

i do have a 404 error like so:

ErrorDocument 404 http://www.jungleravers.com

but it does do anything in some places, eg:
http://jungleravers.com/ravereviews/reviews/nottingham/

CptAwesome's picture

He has: 370 posts

Joined: Dec 2004

thats why you need to have an htaccess file that says

-indexes

OR

DirectoryIndex /index.html (or whatever the front page file name is)

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.