如何使用 Apache 将多个域重定向到不同的子目录

发布于 2024-12-11 06:24:23 字数 333 浏览 0 评论 0原文

我想将多个域重定向到不同的子目录。例如:

如果访问者使用域 www.cookie.com,该域具有到 www.mainme.com 的 HTTP 重定向,那么当访问者到达那里时,Apache 会检测到该域并将其重定向到 mainme.com/cookie

对于 puppy.com 也是如此。如果 HTTP 重定向到 mainme.com,当用户到达那里时,它会被重定向到 mainme.com/puppy

这可以在 .htaccess 中完成吗?

使用 Linux 作为服务器,并使用 /var/www 作为 Web 文件夹。

现在没有使用虚拟主机,但正在学习如何使用。

I want to redirect several domains to different sub directories. For example:

If a visitor uses the domain www.cookie.com which has a HTTP redirect to www.mainme.com then when the visitor gets there, Apache detects the domain and redirects it to mainme.com/cookie

Same for puppy.com. If it gets HTTP redirected to mainme.com, when the user gets there it gets redirected to mainme.com/puppy

Can this be done in .htaccess?

Using Linux as server with /var/www as web folder.

Not using Virtualhost right now but learning how to do it.

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

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

发布评论

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

评论(2

猫七 2024-12-18 06:24:23

我真的不知道你的意思

转发到 www.mainme.com

如果它通过 HTTP 重定向转发,您需要根据您的需要更改该重定向(因此将 /cookie/puppy 附加到结束)。

如果您的域共享相同的文档根目录,您需要将此代码放入您的 .htacces 中:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?cookie\.com$
RewriteRule ^(.*)$ http://www.mainme.com/cookie? [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?puppy\.com$
RewriteRule ^(.*)$ http://www.mainme.com/puppy? [R,L]

编辑:
因此,您可能会将 http-referer 作为用户来自何处的线索。试试这个:

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http[s]?://(www\.)?cookie\.com(.*)?$
RewriteRule ^(.*)$ http[s]?://www.mainme.com/cookie? [R,L]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?puppy\.com(.*)?$
RewriteRule ^(.*)$ http://www.mainme.com/puppy? [R,L]

I don't really know what you mean by

which is forwarded to www.mainme.com

If it get's forwarded with a HTTP-redirect you need to change that redirect to your needs (so attach /cookie or /puppy at the end).

If your domains are sharing the same documentroot you need to place this code in your .htacces in it:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?cookie\.com$
RewriteRule ^(.*)$ http://www.mainme.com/cookie? [R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?puppy\.com$
RewriteRule ^(.*)$ http://www.mainme.com/puppy? [R,L]

EDIT:
So you might have the http-referer as clue where the user came from. Try this:

RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http[s]?://(www\.)?cookie\.com(.*)?$
RewriteRule ^(.*)$ http[s]?://www.mainme.com/cookie? [R,L]
RewriteCond %{HTTP_REFERER} ^http://(www\.)?puppy\.com(.*)?$
RewriteRule ^(.*)$ http://www.mainme.com/puppy? [R,L]
晨敛清荷 2024-12-18 06:24:23

我希望我理解正确,但如果“转发”指的是 HTTP 重定向,那么您所说的其余内容将不起作用。如果您已经通过将所有信息重定向到 www.mainme.com 来丢弃该信息,那么 Apache 就无法检测到该域。 (HTTP 引用不可靠,因为它可以关闭(我已关闭),但我不确定它的重定向含义是否是您所需要的,或者它是否在浏览器之间保持一致。)

我不确定“告诉域名卖家”是什么意思。是人,还是只是自己在注册商的控制面板中编辑设置?但我想无论哪种方式,如果你可以告诉他们做一件事,你就可以告诉他们做其他事情,所以:

最简单的解决方案可能只是重定向到不同的 URL,而不是全部重定向到相同的 URL。即直接访问 www.mainme.com/sitename。或者,如果您只能转发到域名而不是完整的 URL,请为每个域名创建一个子域:sitename.mainme.com,如果您想将其重定向到 www.mainme.com/,则可以再次重定向。站点名称。

另一种方法是将每个域的 IP 设置为您的 mainme.com 服务器,并将它们作为虚拟主机正常托管。如果您愿意,他们可以重定向,但您也可以直接从 cookie/puppy/etc.com 提供内容。

I hope I understood you right, but if by "forwarded" you mean an HTTP redirect, then the rest of what you say won't work. There's no way for Apache to detect the domain if you've already discarded that information by redirecting all to just www.mainme.com. (The HTTP referer not reliable because it can be turned off (I have it off) but I'm not sure its meaning with redirects is even what you'd need for this, or if it's consistent across browsers.)

I'm not sure what you mean by "tell the domain seller". Is that people, or just editing the settings yourself in your registrar's control panel? But I guess either way if you can tell them to do one thing you can tell them to do something else, so:

The easiest solution is probably to just redirect to different URLs instead of all to the same URL. I.e., directly to www.mainme.com/sitename. Or if you can only forward to a domain name and not a full URL, make a sub-domain for each: sitename.mainme.com, which could then redirect a second time if you want to get it to www.mainme.com/sitename.

The other way to do it is to set the IP of each domain to your mainme.com server, and just host them all normally as virtual hosts. They could redirect if you want but you could also just serve content directly from cookie/puppy/etc.com.

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