Php .htaccess-如果文件不';不存在

Php .htaccess-如果文件不';不存在,php,apache,.htaccess,mod-rewrite,redirect,Php,Apache,.htaccess,Mod Rewrite,Redirect,我的.htaccess文件包含以下指令 DirectoryIndex index.html index.php # redirect invalid requests and missing files to the home page RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ http://www.mydomain.com/ [L] 问题

我的
.htaccess
文件包含以下指令

DirectoryIndex index.html index.php  
# redirect invalid requests and missing files to the home page  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*)$ http://www.mydomain.com/ [L]
问题是许多程序员(松散地使用术语)都在这个网站上工作。有些目录使用
index.html
,有些目录使用
index.php

如果目录使用
index.php
,则对
www.mydomain.com/directory
的请求将查找
www.mydomain.com/directory/index.html
,并重定向到
www.mydomain.com
,然后才能查找
www.mydomain.com/directory/index.php

如何尝试所有
DirectoryIndex
文件并将丢失的文件重定向到主页

如何尝试所有DirectoryIndex文件并重定向丢失的文件 到主页

我认为mod_rewrite或mod_alias模块不可能实现这一点。你必须寻找其他方法来解决这个问题。下面是一个使用相同的DirectoryIndex指令强制加载根目录下的文件作为最后一个选项的想法:

将此行放在根目录下的一个.htaccess文件中:

DirectoryIndex  index.php  index.html  /missing.php
使用以下代码行在根目录中创建
missing.php

<?php
header("Location: http://www.example.com/"); // Redirect
?>
注:
1. <规则中的code>/index.php只是一个示例。它可以是任何文件。

2.对于永久重定向,请将子目录中的[L,NC]替换为[R=301,L,NC]。

ovverride
.htaccess
替换为
DirectoryIndex.php
DirectoryIndex.html
?我认为这是一个可行的选项,但如果可能,我更愿意单独使用.htaccess处理。关于如何在不丢失.php重定向的情况下进行处理,有什么想法吗?正如我在编辑后的回答中所说,我认为使用mod_rewrite指令无法实现这一点。我建议的内容确实放在根目录下的.htaccess文件中。我知道PHP比mod_rewrite快得多,在这种情况下更有效。最后,需要使用missing.php重定向。如果请求的是目录,那么您的解决方案可以正常工作。但是如何处理缺少的文件,例如example.com/file-not-on-server.php
# Directive that solves the original question
DirectoryIndex  index.php  index.html  /missing.php

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Next condition is met when the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
# If previous condition was met, use the next rule
RewriteRule ^(.*)$         /index.php [L,NC]