【发布时间】:2020-05-13 01:18:44
【问题描述】:
我知道有人问过类似的问题,但在我的一生中,我似乎无法让它发挥作用。
这个想法是将子域重写为 url 的一部分。我们还希望服务器在不进行远程调用的情况下处理请求,并且不重写客户端中的 url。
简单地说:
http://test.example.com/(something) 应该被解释为http://example.com/test/(something)
“某事”是可选的。
我尝试了很多不同的解决方案,但似乎从来没有完全正确 - 这是我第一次体验。
# RewriteEngine on
# RewriteCond %{HTTP_HOST} ^test.example.com [NC]
# RewriteRule ^((?!test/).*)$ /test/$1 [L,NC]
# RewriteCond %{HTTP_HOST} ^test\.example\.com$ [NC]
# RewriteRule ^(.*)$ "test%{REQUEST_URI}"
# RewriteRule ^(.*)$ test/$1 [L]
#RewriteEngine on
#RewriteCond %{HTTP_HOST} ^test.example.com [NC]
#RewriteRule ^(.*)$ example.com/test/%{REQUEST_URI} [P,L,NC]
重要的是要知道example.com/test/something 已经存在于不同的服务器上,不应使用或调用。
我的想法是将test.example.com重定向到新服务器,然后在新服务器上使用另一个虚拟主机将其解释为example.com/test(整个应用程序正在开发没有子域,但我们将逐步迁移使用子域,直到我们设法迁移所有内容并恢复“正常”方式)。
随着时间的推移,我们将拥有 sub1.example.com、sub2.example.com 等。别名可能会在这里发挥作用,但我的深度不够。
感谢您的帮助!
【问题讨论】:
-
您是否有权访问子域的服务器配置/虚拟主机?接受初始请求(到
test.example.com)的服务器是否托管其他域? (只是想知道为什么当目标位于不同的服务器上时,您要显式检查请求的主机名?) -
@MrWhite 我认为最好的答案是我不知道自己在做什么,这就是我明确检查主机名的原因——目前这一切都是实验。最后,服务器将只托管 sub1.example.com、sub2.example.com(待重写)、admin.example.com(未重写)和将来 example.com。 Example.com 目前在其他地方。我现在确实可以访问我想要的所有内容 - 我无法在没有子域的情况下公开 example.com。
标签: apache .htaccess mod-rewrite vhosts