mod_rewrite:将分层子域转换为分层子目录

发布于 2024-08-21 09:59:05 字数 643 浏览 10 评论 0原文

请考虑以下问题。我已经得到了带有分层子域的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半世晨晓 2024-08-28 09:59:05

4 级子域的可能解决方案:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.tld
RewriteRule (.*) %1./$1 [R,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %2./%1./$1 [R,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %3./%2./%1./$1 [R,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %4./%3./%2./%1./$1 [R,L]

谢谢。

--
恩克德

Possible solution for 4 levels of sub-domains:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.tld
RewriteRule (.*) %1./$1 [R,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %2./%1./$1 [R,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %3./%2./%1./$1 [R,L]

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.domain\.tld
RewriteRule (.*) %4./%3./%2./%1./$1 [R,L]

Thank you.

--
nkd

﹎☆浅夏丿初晴 2024-08-28 09:59:05

之前的解决方案以非常有趣的无限重定向循环结束。这是我现在得到的一个解决方案(不是很优雅,但它有效;但有一个巨大的“但是”):

# Working solution for five levels of sub-domains

RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC]

RewriteRule (.*) http://DOMAIN.TLD/%5./%4./%3./%2./%1./$1 [R,L]

有人可以向我解释为什么它有效吗?它确实有效,我对其进行了广泛的测试。但为什么它实际上有效呢?如果我查看 RewriteRule 行,我怀疑它应该有效...谢谢您的解释。

顺便说一句:如果上述五个重写条件和规则有效,看起来可以用某种“两行”(一个条件加一个规则)重写。我已经尝试过,使用上述规则和以下条件而不是上面给出的五个条件:

RewriteCond %{HTTP_HOST} ^(([^\.]+)\.)+DOMAIN\.TLD [NC]

并进行了一些尝试,但没有真正成功。感谢所有如何简化的想法
重写东西并使其更加“理智”(如果可能的话)。

--
恩克德

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'):

# Working solution for five levels of sub-domains

RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.([^\.]+)\.DOMAIN\.TLD [NC]

RewriteRule (.*) http://DOMAIN.TLD/%5./%4./%3./%2./%1./$1 [R,L]

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:

RewriteCond %{HTTP_HOST} ^(([^\.]+)\.)+DOMAIN\.TLD [NC]

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

杀お生予夺 2024-08-28 09:59:05

It is possible if you send the part to be reversed to an external script via RewriteMap and have that handle it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文