使用 .htaccess 重定向到动态相对路径? [英] Redirect to dynamic relative paths with .htaccess?
问题描述
是否可以让 .htaccess理解"动态相对路径并正确重定向到它们?
Is it possible to make .htaccess "understand" dynamic relative paths and redirect to them properly?
我的设置如下:
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
部分在文件系统上没有任何位置作为物理"文件夹或文件.
And so on. For the sake of the example, "htroot" contains the .htaccess I need to modify. The following sublevels (aaa, bbb, ccc) can be any a-z0-9 name, and the folders have an index.php (or any other .php to be redirected to). The xyz
should work as a parameter of sorts, see next part. The xyz
part is nowhere on the filesystem as a "physical" folder or file.
我需要实现的是以下内容:当您使用 url 访问时
What I need to achieve is the following: When you access with the 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
,自然.
where the index.php kicks in -> I can get the xyz
parsed from REQUEST_URI and process it to serve the correct content that it specifies, while the URL of the page stays as http://domain.com/htroot/aaa/xyz
, naturally.
到目前为止,如果每个子级别(aaa 等)都有自己的 .htaccess,我已经设法实现了这一点,但是我需要一个在 htroot
中只有一个 .htaccess 的地方处理这个.我猜这可能与 .htaccess 中的 $0 参数有关,但不确定.
So far I have managed to pull this off if every sublevel (aaa etc.) has it's own .htaccess, but I would need one where there is only a single .htaccess located in htroot
that handles this. I'm guessing it might have something to do with the $0 parameters in .htaccess, but not sure.
推荐答案
您可能想要执行以下操作:
You may want to perform something like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ /htroot/$1/index.php?data=$2 [L]
如果第一个通配符匹配 (.*)
是 aaa
并且第二个通配符匹配 (.*)
是 xyz
(htroot/aaa/xyz) 它将从
If the first wildcard match (.*)
is aaa
and the second wildcard match (.*)
is xyz
(htroot/aaa/xyz) it will get the content from
htroot/aaa/index.php?data=xyz
htroot/aaa/index.php?data=xyz
并且您将能够使用 $_GET['data']
这篇关于使用 .htaccess 重定向到动态相对路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!