Apache 和 mod_rewrite:将域重定向到子目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/775868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apache and mod_rewrite: Redirect domain to subdirectory
提问by ObiWanKenobi
(I know this is not a programming question per se, but it involves regular expressions, so at least it is borderline... )
(我知道这本身不是一个编程问题,但它涉及正则表达式,所以至少它是边缘......)
The setup:
设置:
Apache 2.0 with mod_rewrite on Windows. Two domains, let's call them domain1.example and domain2.example. I would like to host both domains on the same server ("server1"), so I point them to the same IP address.
Windows 上带有 mod_rewrite 的 Apache 2.0。两个域,我们称它们为 domain1.example 和 domain2.example。我想在同一台服务器(“server1”)上托管两个域,所以我将它们指向同一个 IP 地址。
Now, if the user types "domain2.example" into his browser, I want him to end up in a subdirectory** on the server, but leave the domain he typed intact ("domain2.example/domain2/"). The redirection must leave all absolute and relative links on pages under this domain/directory intact, of course.
现在,如果用户在他的浏览器中键入“domain2.example”,我希望他最终进入服务器上的一个子目录**,但保持他键入的域完好无损(“domain2.example/domain2/”)。当然,重定向必须保持该域/目录下页面上的所有绝对和相对链接完整无缺。
Is this possible with mod_rewrite (or Apache virtual hosts or other method), and how do I do it?
这是否可以使用 mod_rewrite(或 Apache 虚拟主机或其他方法),我该怎么做?
** The "subdirectory" in this case is not actually a file folder on disk, but a virtual folder made with the Apache "Location" directive.
** 在这种情况下,“子目录”实际上不是磁盘上的文件夹,而是使用 Apache“位置”指令创建的虚拟文件夹。
Thanks.
谢谢。
采纳答案by Dana the Sane
I don't believe that you need to use mod_rewrite, you should be able to use vhosts for this like you suggest. To do this, you will have a single vhost with servername domain2.example which points to the directory you want. This will also use the ServerAlias directive for domain1.example so requests for this go to the same directory.
我不相信你需要使用 mod_rewrite,你应该能够像你建议的那样使用 vhosts。为此,您将拥有一个服务器名为 domain2.example 的虚拟主机,它指向您想要的目录。这还将对 domain1.example 使用 ServerAlias 指令,因此对此的请求将转到同一目录。
See the documentation for ServerAliasand DocumentRoot. Also note that if you want the directory to show up in the URL, you willneed to use mod_rewrite.
请参阅ServerAlias和DocumentRoot 的文档。另请注意,如果您希望目录显示在 URL 中,则需要使用 mod_rewrite。
回答by Bill Fraser
I'm assuming you don't have access to the Apache configuration, otherwise, yes, virtual hosts are your best option. If you don't, however, this can be put in a .htaccess file and should do the trick:
我假设您无权访问 Apache 配置,否则,是的,虚拟主机是您的最佳选择。但是,如果您不这样做,则可以将其放入 .htaccess 文件中,并且应该可以解决问题:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.+)\.yourwebsite\.com$
RewriteRule ^/(.*)$ /path/to/your/subdomains/%1/ [L]
So a request to http://foo.yourwebsite.com/barwould go to /path/to/your/subdomains/foo/bar
所以一个请求http://foo.yourwebsite.com/bar会去/path/to/your/subdomains/foo/bar
回答by Peter Smit
Assuming that the domains are independend the advised solution is virtual hosts.
假设域是独立的,建议的解决方案是虚拟主机。
You can find the documentation at the apache website.
您可以在apache 网站上找到该文档。

