重定向到请求的子文件夹的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引擎对其进行解析。