Php 如何创建设置而不是全局重定向?

Php 如何创建设置而不是全局重定向?,php,redirect,Php,Redirect,我需要,当一个人来到mysite.com/index.html->refrect to mysite.com/index.php或 mysite.com/index.phtml->mysite.com/index.php。或 mysite.com/index.sdsf->mysite.com/index.php。或 mysite.com/about.phtml->mysite.com/about.php 您希望所有传入的请求都使用php指向/index.php 你可以试试 if ( ! preg

我需要,当一个人来到mysite.com/index.html->refrect to mysite.com/index.php或

mysite.com/index.phtml->mysite.com/index.php。或 mysite.com/index.sdsf->mysite.com/index.php。或


mysite.com/about.phtml->mysite.com/about.php

您希望所有传入的请求都使用php指向
/index.php

你可以试试

if ( ! preg_match('/\/^index\.php/', $_SERVER['REQUEST_URI'])) {
   location('Header: /index.php');
   exit;
}
注意,您应该在重定向中使用完整的URL


否则,如果您想选择您专门提到的那些,您可以构建它们的数组,然后循环检查。

为什么需要重定向它?为什么不直接使用index.php作为默认页面

无论如何,这样做的一个好方法是使用
header

header('Location: http://mysite.com/index.php');

希望这能有所帮助。

如果您想捕获不正确的文件名,那么这是您必须在PHP之外做的事情。尝试一个mod_重写规则,如:

 RewriteCond  %{REQUEST_FILENAME}  !-f
 RewriteRule  ^index\.(?!php)\w+$  index.php [L,R]