Jump to content
xProteuSx

Redirect for Directories without index.html

Recommended Posts

I have a website with a series of directories that are created "on the fly".  These images are used exclusively for storage of image files, and I've written a script that copies a default index.html file to each directory as it is created.  This index.html file has nothing but a redirect script, so that if someone tries to access the directory instead of a file listing they are sent to the home page of the website.

 

Is there any way to achieve this using the .htaccess file?  Is there any way to get around having to copy this silly index.html file to every single directory?

 

I don't even know how to phrase this issue for Google ...

Share this post


Link to post
Share on other sites

It's just a form of URL rewriting. Every time someone requests the directory itself you redirect them somewhere else. Looks like

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [F]
which will actually send a 403 Forbidden if they request something that is not a file (ie, is a directory or does not exist). IMO that's better than a redirect. If you really want to redirect then just change that RewriteRule accordingly, making sure it sends a permanent redirect (R=301) instead of the default temporary redirect.

 

Oh, and that would go in a .htaccess within the first image folder: if your images go in /images/* then you'd put it in /images/.htaccess.

Share this post


Link to post
Share on other sites

You could also just disable indexes for the images folder by placing this in the .htaccess:

Options -Indexes
With that Apache will serve a forbidden response if someone tries to access the directory, while still serving the files or 404 not found as appropriate.

 

Of course with either solution, you'll have to remove any existing index.html files to prevent Apache from detecting and serving them.

  • Like 1

Share this post


Link to post
Share on other sites

Or you could move these directories to a location that is not publicly accessible and implement a simple "download script" that allows users to access the files. By using a download script you can also implement authentication/logging/ext to add additional security measures if warranted.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.