简体   繁体   中英

Directory index page precedes htaccess

What I want is that only if my root domain is accessed meaning URI is empty, it gets rewritten without redirect, otherwise rewrite to index.php if not accessing file or folder. My .htaccess looks like this:

RewriteEngine On
RewriteBase /
RewriteRule ^/?$ /mypages/landing_page.html [QSA,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

It works as expected on some servers but on another it works only if there is no index.php file in folder. Something like directory indexing preceding htaccess rules? If I delete the index.php or rename it to index2.php and change my htaccess rules accordingly rewrite works as expected.

Any ideas what might be causing the server to behave like that?

There was a bug introduced in Apache 2.4.2 ( #53929 ) that has the same symptom you're describing:

"The issue is caused by the DirectoryIndex directive. mod_dir is not respecting the result of the rewrite execution. If DirectoryIndex is set to disabled, it starts working correctly."

The issue is fixed in Apache 2.4.9.

If you have a mixed environment (ie pre and post Apache 2.4.2 versions), this could also explain why your Rewrite is working correctly on some servers, but not others.

If upgrading is not an option, you can also workaround the issue using a conditional SetHandler (based on an env var set in your RewriteRule). This will disable DirectoryIndex for just those requests.

The bug itself (and workaround example) is documented in the following link:

https://bz.apache.org/bugzilla/show_bug.cgi?id=53929

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM