繁体   English   中英

htaccess将子目录重定向到php文件

[英]htaccess redirect URL with sub directories to php file

我需要将具有未知子目录的特定根URL(在此示例中为“test”)重定向到php文件。

示例网址

http://domain.com/test
http://domain.com/test/sub1
http://domain.com/test/sub1/sub2

我试过这个.htacces代码

RewriteRule ^test(/.*)?$ redirect.php?path=$1

“编辑”

我使用此代码拆分$ path

$path = explode("/", $_GET['path']);

但是当我在redirect.php文件中的$ path上运行print_r时,数组0为空

Array ( [0] => [1] => sub1 [2] => sub2 )

如何获得阵列中的完整路径?

要在PHP中访问GET变量,您应该使用以下内容:

$path = $_GET['path']; echo $path;

http://php.net/manual/en/reserved.variables.get.php

如果要捕获完整路径,请使用RewriteCondREQUEST_URI变量:

RewriteCond %{REQUEST_URI} ^/(test(?:/.*)?)$ [NC]
RewriteRule ^ redirect.php?path=%1 [L,QSA]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM