将多个子域重定向到另一个域名

发布于 2025-01-08 11:54:58 字数 835 浏览 5 评论 0原文

有一个关于使用 mod_rewrite 重定向特定子域以使用另一个域的问题。我正在寻找最短的方法来执行此操作,而不需要为每个域名创建单独的重写规则。我将添加许多新域名(总共大约 20-30 个域名)。

假设我的主域名是 example.com,我想使用该域名进行所有操作。因此,如果使用我的任何其他域,它们将自动重定向到主域,并保留子域前缀和 URL 路径。

示例:
test.example.org => test.example.com
test2.example.co.uk => test2.example.com
test3.example.net/hello/world.php => test3.example.com/hello/world.php

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.([a-zA-Z]+)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1\.example.com/$1 [R=301,L]

我不确定上面的内容是否可以正常工作或者语法是否正确。但基本上,我希望它匹配子域前缀,然后是任何域名和任何 TLD(.org、.info、.biz、.co.uk、.net 等)。我认为首先需要确保它不是正确的主域(example.com),以防止无限重定向循环。

另外,是否可以检查 HTTPS 是否打开或关闭并正确设置重定向?如果没有,我可以随时将 HTTPS 设置为“开”。

对于这种混乱,我深表歉意,尽管我想确保我第一次就得到了正确的结果,而不需要将每个域都编程到 .htaccess 中。

谢谢! :)

Got a question about using mod_rewrite to redirect specific subdomains to use another domain. I am looking for the shortest possible way to do this without the need to create a separate rewrite rule for each of my domain names. I will be adding many new domain names (roughly 20-30 domains total).

So let's say my main domain name is example.com and I want to use that domain name for everything. So if any of my other domains are used, they will automatically be redirected to the main domain, preserving the subdomain prefix and the URL path.

Example:
test.example.org => test.example.com
test2.example.co.uk => test2.example.com
test3.example.net/hello/world.php => test3.example.com/hello/world.php

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.([a-zA-Z]+)\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1\.example.com/$1 [R=301,L]

I am unsure if the above will work correctly or if it is proper syntax. But basically, I would like to have it match the subdomain prefix, then any domain name, and any TLD (.org, .info, .biz, .co.uk, .net, etc.). I would assume it would need to make sure that it is not the correct main domain (example.com) first to prevent a infinite redirect loop.

Also, is there a possibility to check if HTTPS is ON or OFF and set the redirect correctly? If not, I can always have HTTPS set to ON.

Sorry for this confusion, although I want to make sure I get this right the first time without needing to program each and every one of the domains into the .htaccess.

Thanks! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

玉环 2025-01-15 11:54:58

(.*) 是贪婪的,所以您不想使用它,并且我假设您不想重新创建无限循环。

RewriteEngine on

RewriteCond   %{HTTPS}s    ^..(s?)
RewriteRule   ^            -                                [E=PROTO:http%1]              

RewriteCond   %{HTTP_HOST} !^\w+\.example\.com$             [NC]
RewriteCond   %{HTTP_HOST} ^(\w+)\.\w+\..+                  [NC]
RewriteRule   ^/?(.*)      %{ENV:PROTO}://%1.example.com/$1 [R=301,L]

(.*) is greedy so you don't want to use that, and I assume that you don't want to recreate an infinite loop.

RewriteEngine on

RewriteCond   %{HTTPS}s    ^..(s?)
RewriteRule   ^            -                                [E=PROTO:http%1]              

RewriteCond   %{HTTP_HOST} !^\w+\.example\.com$             [NC]
RewriteCond   %{HTTP_HOST} ^(\w+)\.\w+\..+                  [NC]
RewriteRule   ^/?(.*)      %{ENV:PROTO}://%1.example.com/$1 [R=301,L]

灯下孤影 2025-01-15 11:54:58

该规则不起作用,因为它会将任何以 example.com 结尾的主机重定向回 example.com,并且您将得到一个重定向循环。您需要将正则表达式更改为如下所示,以匹配.com结尾的任何内容:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.?([a-zA-Z]+)\.(?:(?!com).)*$ [NC]

That rule will not work because it will redirect any host ending with example.com back to example.com and you'll get a redirect loop. You need to change the regex to look like this, to match anything that doesn't end with .com:

RewriteCond %{HTTP_HOST} ^([^\.]+)\.?([a-zA-Z]+)\.(?:(?!com).)*$ [NC]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文