gpt4 book ai didi

Apache 和 mod_rewrite : Redirect domain to subdirectory

转载 作者:行者123 更新时间:2023-12-02 09:28:48 26 4
gpt4 key购买 nike

(我知道这本身不是一个编程问题,但它涉及正则表达式,所以至少它是边界......)

设置:

Windows 上带有 mod_rewrite 的 Apache 2.0。两个域,我们称它们为domain1.example 和domain2.example。我想将两个域托管在同一台服务器(“server1”)上,因此我将它们指向相同的 IP 地址。

现在,如果用户在浏览器中输入“domain2.example”,我希望他最终位于服务器上的子目录**中,但保持他输入的域不变(“domain2.example/domain2/”) 。当然,重定向必须保持此域/目录下页面上的所有绝对和相对链接完好无损。

这可以通过 mod_rewrite (或 Apache 虚拟主机或其他方法)实现吗?我该怎么做?

** 本例中的“子目录”实际上并不是磁盘上的文件夹,而是使用 Apache“Location”指令创建的虚拟文件夹。

谢谢。

最佳答案

我假设您无权访问 Apache 配置,否则,虚拟主机是您的最佳选择。但是,如果您不这样做,可以将其放入 .htaccess 文件中,并且应该可以解决问题:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.yourwebsite\.com$
RewriteRule ^/(.*)$ /path/to/your/subdomains/%1/$1 [L]

因此请求<a href="http://foo.yourwebsite.com/bar" rel="noreferrer noopener nofollow">http://foo.yourwebsite.com/bar</a>会转到/path/to/your/subdomains/foo/bar

关于Apache 和 mod_rewrite : Redirect domain to subdirectory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/775868/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com