Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重定向到请求的子文件夹的index.php_Php_Apache_.htaccess_Redirect - Fatal编程技术网

重定向到请求的子文件夹的index.php

重定向到请求的子文件夹的index.php,php,apache,.htaccess,redirect,Php,Apache,.htaccess,Redirect,我需要重定向到所请求目录的index.php 因此,我需要: http://www.site.com/folder/files/hello.php 要重定向到: http://www.site.com/folder/files/index.php http://www.site.com/folder/files/pages/other/index.php 对于任何子文件夹也一样: http://www.site.com/folder/files/pages/other/hello.php

我需要重定向到所请求目录的
index.php

因此,我需要:

http://www.site.com/folder/files/hello.php
要重定向到:

http://www.site.com/folder/files/index.php
http://www.site.com/folder/files/pages/other/index.php
对于任何子文件夹也一样:

http://www.site.com/folder/files/pages/other/hello.php
重定向到:

http://www.site.com/folder/files/index.php
http://www.site.com/folder/files/pages/other/index.php
(技术上)正确的方法 构造一个URI,包含当前方案(
http
/
https
/etc)、当前文件的主机名和路径,并发出位置头

这是因为位置头URI应该是完整和绝对的。相对URI在技术上是不允许的,但是有一个草案规范集来改变这一点

务实之道 只需发出一个相对位置标题,因为它很可能会工作

header('Location: index.php');
exit;

这可以在.htaccess中完成,因此不需要任何PHP代码

通过
httpd.conf
启用
mod_rewrite
.htaccess
,然后将此代码放入
文档根目录下.htaccess
文件:

RewriteEngine On
RewriteBase /

RewriteRule (.+?)/[^./]+\.php$ /$1/index.php [L,NC]

要将
文件夹/files/hello.php
重定向到
文件夹/files/index.php
请在
文件夹/files/hello.php
文件中使用以下行

<?php

$URL =  'http://'.$_SERVER['SERVER_NAME'].'/folder/files/index.php';

header("Location:  $URL ");

?>


下次重定向时使用类似的语法。

对于上面的所有PHP代码,请用:“”括起来,并确保文件以.PHP结尾,以便PHP引擎对其进行解析。