简体   繁体   中英

redirect subfolder/index.php to subfolder/

I'm trying to redirect any requests to subfolder/index.php to subfolder/ and still display the contents of the subfolder's index.php file. This isn't specific to just one subfolder so I need a general rule. But note that some directories, such as css/ and images/ won't have an index.php file and I'd just want to deny access to them if the user tried to hit those specific directories.

At the moment, a request to subfolder/index.php goes to subfolder/index/ (but does still actually show the contents of index.php). This is due to the rule I have to replace .php extensions with a trailing slash (thanks to the other posts on this forum!)

So I think I'm just missing a rule that sits above my trailing slash rule.

So far I have this:

RewriteEngine On

# make all requests to the domain root show contents of index.php
DirectoryIndex index.php
#Permanently move requests to index.php to the domain root
RewriteRule ^index\.php$ / [NC,R=301,L]

#do not allow a directory listing
Options -Indexes


##**** INSERT RULE HERE TO DEAL WITH SUBFOLDER INDEX FILES****


## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L]

# add a trailing slash    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Any help would be much appreciated as I've been struggling with the htaccess file for a while and can't work out the answer.

Comment out this rule:

RewriteRule ^index\.php$ / [NC,R=301,L]

And insert this rule in same place:

RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

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