mod_rewrite:将分层子域转换为分层子目录
请考虑以下问题。我已经得到了带有分层子域的domain.tld,如下所示:
a.domain.tld
b.a.domain.tld
c.b.a.domain.tld
... etc.
在Web根目录中还有假设的目录结构:
/foo
/a.
/a./b.
/a./b./bar
/a./b./c.
... etc.
我想实现这样的重写,这将导致Apache以您在下面看到的方式提供目录服务。
a.domain.tld --> /a.
b.a.domain.tld --> /a./b.
c.b.a.domain.tld --> /a./b./c.
... etc.
没有尾随点字符的目录将表现为正常的子目录。
domain.tld/foo/ --> /foo
a.b.domain.tld/bar --> /a./b./bar
我无法使用 mod_vhost_alias,并且如果仅使用 mod_rewrite 可以实现解决方案,我会很高兴。这样的重写有可能实现吗?
感谢您的所有建议。
--
恩克德
Consider the following problem, please. I have got domain.tld with hierarchical sub-domains like the following:
a.domain.tld
b.a.domain.tld
c.b.a.domain.tld
... etc.
There is also hypothetical directory structure in the web-root:
/foo
/a.
/a./b.
/a./b./bar
/a./b./c.
... etc.
I would like to achieve such rewrite that would cause Apache to serve directories in a way you see below.
a.domain.tld --> /a.
b.a.domain.tld --> /a./b.
c.b.a.domain.tld --> /a./b./c.
... etc.
Directories without trailing dot character would behave as normal sub-directories.
domain.tld/foo/ --> /foo
a.b.domain.tld/bar --> /a./b./bar
I can not use mod_vhost_alias and would be glad if the solution was achievable using mod_rewrite only. Is it possible to achieve such rewrite?
Thank's for all your advices.
--
nkd
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
4 级子域的可能解决方案:
谢谢。
--
恩克德
Possible solution for 4 levels of sub-domains:
Thank you.
--
nkd
之前的解决方案以非常有趣的无限重定向循环结束。这是我现在得到的一个解决方案(不是很优雅,但它有效;但有一个巨大的“但是”):
有人可以向我解释为什么它有效吗?它确实有效,我对其进行了广泛的测试。但为什么它实际上有效呢?如果我查看 RewriteRule 行,我怀疑它应该有效...谢谢您的解释。
顺便说一句:如果上述五个重写条件和规则有效,看起来可以用某种“两行”(一个条件加一个规则)重写。我已经尝试过,使用上述规则和以下条件而不是上面给出的五个条件:
并进行了一些尝试,但没有真正成功。感谢所有如何简化的想法
重写东西并使其更加“理智”(如果可能的话)。
--
恩克德
The previous solution ends in very funny infinite redirect loop. Here's a solution I got now (not very elegant, but it works; but with a huge 'but'):
Can somebody explain to me why (the hell) it works? It really does work, I tested it extensively. But why does it work actually? If I look at the RewriteRule line I doubt it should work... Thank you for your explanations.
BTW: If the above five rewrite conditions and rule work, it looks like it could be re-written in some sort of 'two-liner' (one condition plus one rule). I tried that already, by using the above rule and the following condition instead of the five conditions given above:
and played with it a little but with no real success. Thanks for all ideas how to simplify
the rewrite stuff and make it more 'sane' (if possible).
--
nkd
如果您通过 将要反转的部分发送到外部脚本,这是可能的
RewriteMap
并让它处理它。It is possible if you send the part to be reversed to an external script via
RewriteMap
and have that handle it.