我有一个 Apache 服务器,上面有两个域(通过 VirtualHost)。
文件设置如下:/example1/example2/
example1
是 www.example1.com
的根
example2
是 www.example2.com
的根
我想要www.example1.com/example2/<whatever else>
重定向到www.example2.com/<whatever else>
我可以访问 Apache 主配置文件。有关如何执行此操作的任何提示?我对配置 Apache 不太熟悉,因此非常感谢任何解释。
与此相关的其他问题缺乏解释,所以我觉得我必须发布自己的问题。
最佳答案
您可以使用 .htaccess 文件快速完成此操作。在 www.example1.com/example2/
中,您将创建此 .htaccess 文件。
<VirtualHost *:80>
...
<Directory /path/to/vhost/>
RewriteEngine on
RewriteBase /example2/
RewriteRule ^(.*)$ http://www.example2.com/$1 [R=301]
</Directory>
</VirtualHost>
这确实假设您希望重定向为 301,并且 example2
文件夹已设置 AllowOverride All
。
此规则使用正则表达式捕获传入 URL(不含 example2
位),然后将其附加到重写的 URL。方括号中的 R 告诉 Apache 使用 Location header 来重写 URL。 =301
告诉 Apache 使用 301 永久重定向 header 。
关于apache - 如何使用 Apache 将目录重定向到另一个域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10263476/