dousi4148 2018-04-20 07:26
浏览 98
已采纳

将子文件夹/ index.php重定向到子文件夹/

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.

  • 写回答

1条回答 默认 最新

  • doujuanju3076 2018-04-20 11:30
    关注

    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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目