Forum Moderators: phranque
Perhaps this question is pretty easily solved.
I'm relatively new to .htaccess, so I haven't figured the logic out, yet.
Would like to find out if it's possible via the .htaccess file to redirect queries for folders with no default index file to home page.
This is what I do now:
DirectoryIndex index.php index.shtml index.html index.htm redirect.shtml
The redirect.shtml file is placed in folders without one of the other mentioned index files. The query is then redirected to home page via a javascript.
I would like the query to be redirected without me having to upload a redirect page to each folder without an index file.
Is this possible?
TIA
Welcome to WebmasterWorld!
Yes, it's possible.
You'll need to use a regular-expressions pattern to recognize the case where no filename is present, and then rewrite or redirect that request to your home page. You'll also need to check that no index file exists in that directory.
Here's an example to get you started. This code will redirect a request with no filename in any directory that does not contain the file index.php to index.php in the Web root directory of your site.
RewriteCond index.php !-f
RewriteRule !\.[a-z]{2,5}$ http://www.yourdomain.com/index.php [R=301,L]
The code above illustates one of two ways to solve the problem, and is the more efficient of the two.
See the references to mod_rewrite and regular expressions in our forum charter [webmasterworld.com].
Jim
Many thanks for your reply and the welcome. :)
I'm all into following the W3C recommended guidelines, which also includes the use of folders as URL structure rather than file names as URL structure. It's really neat when I decide to change the underlying technology, like from .shtml to .php etc.
Thanks for the tip anyway. That was very kind of you.
I've read the charter you linked to and am all in favor of doing my own studies. I have read the Apache module documentation on mod_rewrite, but some things are still unclear to me and some things are not even mentioned there I have discovered.
Perhaps you know of a more thorough tutorial, or are many of the codes tricks of the trade? :)
Thanks for the code piece, I'll work on that. I still need to understand much of the letters and numbers within brackets sort of stuff, but I do get the rough picture outlined when it comes to some of the commands.