Apache, RewriteRule, and DirectoryIndex


Well-Known Member
Aug 21, 2005
A client of ours recently had a problem with RewriteRules and I wanted to share this little oddity with everyone. This client has 2 servers with us. The first is running Apache 1.x and the second is running Apache 2.x.

On the first server, the client was used to setting up his designs in a subdirectory of the public_html/ folder. He would then create a redirect in cPanel to redirect index.html to <subdirectory>/index.php. This worked fine for him, including redirecting all accesses to his domain alone (i.e. no index.html in request URI) to the desired subdirectory.

Upon receiving his second machine with us, he complained that this no longer worked.

After investigating, I found that Apache 1.x appears to parse the RewriteRules after loading the DirectoryIndex. Visitors would visit the main domain, Apache would load the DirectoryIndex, and then the RewriteRule pertaining to the corresponding index would match, forcing the redirect. I tried removing the index and the redirect would fail to trigger, only with the index in place would the redirect match. I guess the Apache devs decided this was not correct behavior since the actual request was not for index.html and in Apache 2.x this behavior does not occur. In Apache 2.x, only requests for the index.html file directly trigger a match.

If you want to redirect access to http://domanname.com to a subfolder via cPanel, put a single question mark in the text box right after the dropdown for the domain name. The question mark basically says match zero or one / after the domain name to trigger a positive match.

The other suggestions I provided to our client were:

1) Use a META refresh. Opponents of this method often site SEO reasons for not using META refreshes, but both Google and Yahoo are reported to view 0 delay META refreshes as 301 redirects. Though, W3C recommends not using these.

2) Put your installs directly in the public_html/ folder. There was no apparent need for the redirect to the subfolder, so the client agreed this made the most sense.