Forum Moderators: phranque
The problem as I understand it is that after the rewrite rule strips the file name from the URL, the URL, lacking a file name, is passed through mod_dir which adds it back on and forces another run through the rewrite rules. The rewrite rules alter it again and the process repeats.
I stumbled upon the environment variable SCRIPT_URL which can be tested to see if the index file was passed to the server in the original request - it is not altered by subsequent URL rewrites. This can be used to only do the rewrite if the file was present in the original request, and skip the rewrite if the file name was added by mod_dir.
RewriteCond %{ENV:SCRIPT_URL} ^(.*)/index.html$
RewriteRule ^(.*)/index\.shtml$ [domain.com$1...] [R=301,L]
I have tried this on my site and it seems to work without causing problems. Considering the other posts on this subject and proposed solutions I am not yet convinced this really works. Can someone check this and let me know if it is a legitimate solution that doesn't break anything?
Thanks,
Kevin
Welcome to WebmasterWorld [webmasterworld.com]!
This sounds like a pretty good work-around! I haven't tried it, though, because I have a question:
In your RewriteCond, you refer to index.html, while in the RewriteRule, you reference index.shtml. While I believe you intended that they both be the same filetype, I'm not sure -- and I would rather not put this up on a live server for testing until I find out.
Thanks,
Jim
My site currently uses a number of default extensions and, if I can get some validation that I am not messing things up, I will try adding the other index variations to the condition and rule. Something like:
RewriteCond %{ENV:SCRIPT_URL} ^(.*)/(index妃ain)\.(html多tm圭gi如hp存html)$
RewriteRule ^(.*)/(index妃ain)\.(html多tm圭gi如hp存html)$ [domain.com$1...] [R=301,L]
What do you think?
Kevin
RewriteCond %{ENV:SCRIPT_URL} /(index妃ain)\.(s?html多tm圭gi如hp)$
RewriteRule ^(.*)/(index妃ain)\.(s?html多tm圭gi如hp)$ http://www.domain.com$1/ [R=301,L]
Jim
One added benefit is that if any "index" or "main" file is passed in that matches the condition it ends up at the correct default index page for that directory using the mod_dir function. This as opposed to a 404 page. Very nice.
Kevin