【问题标题】:htaccess removing index.php only root folderhtaccess 仅删除 index.php 根文件夹
【发布时间】:2025-12-11 12:45:01
【问题描述】:

成熟了一个我无法处理的问题,不得不寻求帮助。 所以...

我已经配置了 .htaccess 文件:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]

此代码删除站点上的所有目录 index.php。我还需要仅在站点根目录中将其删除。问题是根可以是任何子目录,如“/”和“/some_folder/” 是否可以在没有明确的 basedir 的情况下做到这一点?

例如对于 CNC,我设法做到了:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . ./ [L]

因此,问题: 站点根目录 = httр://site.com/sub/ 删除 index.php 只需要在根目录下。 Rot 目录可能会有所不同,并且该决定应该没有明确的 basedir

【问题讨论】:

  • 我不清楚你想做什么。换句话说,输入到底应该是什么,输出应该是什么?
  • 站点根目录 = httр://site.com/sub_dir/;输入 = /site.com/sub_dir/index.php;输出 = /site.com/sub_dir/; input2 = /site.com/sub_dir/sub2/index.php;输出2 = /site.com/sub_dir/sub2/index.php;

标签: .htaccess web root


【解决方案1】:

您可以使用此规则仅从根目录中删除 index.php:

RewriteCond %{THE_REQUEST} \s/index\.php [NC]
RewriteRule ^index\.php$ / [L,R=301,NC]

【讨论】:

  • 不幸的是,如果目录是子文件夹,它就不起作用
  • 但是你想删除 index.php 只有当它是 root 或者我误解了它?
  • 是的,但在我的情况下,根目录是它所在的位置 htaccess,并且该站点可能具有 htaccess 和子目录,然后转移到根目录的子文件夹。
  • 你的意思是这个.htaccess被放在了一个子目录中? root 始终是 domain.com/index.php 而不是 domain.com/sub/index.php 你能澄清一下你到底想从这条规则中得到什么吗?
  • .htaccess 放在同一个目录下,这被认为是运行站点的起点(主要放在 index.php 的位置),就在这个目录下,必须退出 index.php