xProteuSx Posted June 20, 2016 Share Posted June 20, 2016 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 ... Quote Link to comment https://forums.phpfreaks.com/topic/301367-redirect-for-directories-without-indexhtml/ Share on other sites More sharing options...
requinix Posted June 20, 2016 Share Posted June 20, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301367-redirect-for-directories-without-indexhtml/#findComment-1533843 Share on other sites More sharing options...
kicken Posted June 20, 2016 Share Posted June 20, 2016 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/301367-redirect-for-directories-without-indexhtml/#findComment-1533846 Share on other sites More sharing options...
xProteuSx Posted June 20, 2016 Author Share Posted June 20, 2016 Thank you very much requinix, kicken. Quote Link to comment https://forums.phpfreaks.com/topic/301367-redirect-for-directories-without-indexhtml/#findComment-1533847 Share on other sites More sharing options...
Psycho Posted June 20, 2016 Share Posted June 20, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/301367-redirect-for-directories-without-indexhtml/#findComment-1533849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.