简体   繁体   中英

htaccess is denying access to index.php as directory index

My .htaccess file:

DirectoryIndex index.php

order allow,deny
<FilesMatch "^(index|testfile)\.php$">
Allow From All
</FilesMatch>

Denies access to the sites root, which would typically open index.php. The only way to load the site is to go to mysite.com/index.php instead of mysite.com If I remove the order it works as expected, but now any files in that directory are accessible.

How do I configure the htaccess to deny all files except index.php but also allow for navigation to mysite.com instead of mysite.com/index.php ?

You can do it like this:

DirectoryIndex index.php
order Deny,Allow

# deny everything except landing page
<FilesMatch ".">
Deny From All
</FilesMatch>

# allow index.php and testfile.php
<FilesMatch "^(index|testfile)\.php$">
Allow From All
</FilesMatch>

Alternate Solution using mod_rewrite :

DirectoryIndex index.php
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !\.(?:css|js|png|jpe?g|gif|ico|tiff)$ [NC]
RewriteRule ^(?!(index|testfile)\.php). - [F,NC]

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