Php 如果找不到索引文件,htaccess将重写到根索引

Php 如果找不到索引文件,htaccess将重写到根索引,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我有一个在某些文件夹中预先存在index.php文件的网站。作为使站点更具动态性的推动的一部分,以及从集中索引文件进行路由,如果在目录中找不到索引(php | htm | html),我希望站点上的所有子目录都重写为root index.php --+ / | /index.php +--+ | + /subdir1 | |--+ | /subdir2 + /subdir2/index.php 因此,subdir1将使用根index.php,而subd

我有一个在某些文件夹中预先存在index.php文件的网站。作为使站点更具动态性的推动的一部分,以及从集中索引文件进行路由,如果在目录中找不到索引(php | htm | html),我希望站点上的所有子目录都重写为root index.php

--+ /
  | /index.php
  +--+
  |  + /subdir1
  |
  |--+
     | /subdir2
     + /subdir2/index.php
因此,subdir1将使用根index.php,而subdir2将使用其目录中的index.php文件

我使用了后备资源,因为这正是我想要的:

#Use root index.php
FallbackResource /index.php
然而,我发现当我使用它时,我会得到以下错误消息

Failed to load resource: net::ERR_INCOMPLETE_CHUNKED_ENCODING
于是我又回到了以前的话题:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [NC,QSA,L]
但我不确定如何排除已经存在索引文件的子目录

^!/index\.(php|htm|html)

类似的情况?

您可以在root.htaccess中使用此规则:

DirectoryIndex index.html index.htm index.php
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{DOCUMENT_ROOT}/$1/index\.html !-f 
RewriteCond %{DOCUMENT_ROOT}/$1/index\.htm !-f 
RewriteCond %{DOCUMENT_ROOT}/$1/index\.php !-f 
RewriteRule ^(.+?)(?:/[^/]+)?/?$ index.php [L]

您可以使用基于Lookaround的regex^/(?!ignoreme | ignoreme2 | ignoremeN)([a-z0-9]+)$谢谢,这在大多数情况下都有效,但在第一级子目录上不起作用。或者我的解释不够清楚<代码>www.example.com/subdir1现在就可以了。是否也可以为.html |.htm文件添加忽略?就像:
RewriteCond%{DOCUMENT_ROOT}/$1/index.(php | htm | html)-f
是否只忽略
索引(php | htm | html)
或任何
.htm |.htm
文件?是的,只忽略索引文件
索引(php | htm | html)
,这似乎不会影响
index.html
index.htm
文件。