gpt4 book ai didi

regex - 使用 .htaccess 重定向到动态相对路径?

转载 作者:行者123 更新时间:2023-12-02 03:31:13 24 4
gpt4 key购买 nike

是否可以使 .htaccess“理解”动态相对路径并正确重定向到它们?

我的设置如下:

http://domain.com/htroot/aaa/xyz
http://domain.com/htroot/bbb/xyz
http://domain.com/htroot/ccc/xyz

等等。为了举例,“htroot”包含我需要修改的.htaccess。以下子级别(aaa、bbb、ccc)可以是任何 a-z0-9 名称,并且文件夹具有 index.php(或要重定向到的任何其他 .php)。 xyz 应该作为某种参数,请参阅下一部分。 xyz 部分在文件系统上不作为“物理”文件夹或文件。

我需要实现的是:当你使用url访问时

http://domain.com/htroot/aaa/xyz

它从以下位置获取内容

http://domain.com/htroot/aaa/ (or http://domain.com/htroot/aaa/index.php, either way)

index.php 启动的地方 -> 我可以获取从 REQUEST_URI 解析的 xyz 并处理它以提供它指定的正确内容,而页面的 URL 保持为 http://domain.com/htroot/aaa/xyz,当然。

到目前为止,如果每个子级别(aaa 等)都有自己的 .htaccess,我已经成功实现了这一目标,但我需要一个在 htroot 中只有一个 .htaccess 的子级别处理这个。我猜测这可能与 .htaccess 中的 $0 参数有关,但不确定。

最佳答案

您可能想要执行如下操作:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /htroot/$1/index.php?data=$2 [L]

如果第一个通配符匹配 (.*)aaa,第二个通配符匹配 (.*)xyz (htroot/aaa/xyz) 它将从

获取内容

htroot/aaa/index.php?data=xyz

您将能够使用$_GET['data']在index.php中获取值xyz

关于regex - 使用 .htaccess 重定向到动态相对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9591100/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com