在 apache/centos Web 服务器上重定向页面的最佳方式

Vit*_*liy 4 web-hosting web-server centos redirect apache-2.2

我有一个 centos 6.5 Web 服务器,通过使用虚拟主机在 1 个 IP 地址上运行 2 个网站。

domain1.com 和 domain2.com - 两者都托管在上面的同一 Web 服务器上。

我需要将大约 40 个页面从域 1 重定向到域 2,“例如”:

domain1.com/page1 -> domain2.com/new-page-1
domain1.com/welcomepage -> domain2.com/new-welcome-page
domain1.com/johnsmith -> domain2.com/elvis
domain1.com/test -> domain2.com/production
Run Code Online (Sandbox Code Playgroud)

*请注意,我重定向的页面不在相同的结构/名称下,它们转到完全不同的结构/名称。

谁能建议我可以/需要做什么来完成这项任务?

编辑 #1 我尝试通过 httpd.conf 文件中的 VirtualHost 部分执行此操作。请参阅下面的我的条目。

<VirtualHost *:80>
 ServerName domain1.com
 ServerAlias www.domain1.com
 RedirectPermanent / http://www.domain2.com/page12345/
</VirtualHost>

<VirtualHost *:80>
 ServerName domain1.com
 ServerAlias www.domain1.com
 RedirectPermanent /AboutUs/Founders http://www.domain2.com/about-us-founders/
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

在上述两个条目中,只有第一个正常工作并正确重定向。第二个是重定向到:http : //www.domain2.com/page12345/AboutUs/Founders 有 什么想法吗?

HBr*_*ijn 5

在这种情况下,最简单的解决方案通常是最好的;将 40 个重定向指令添加到 domain1 VirtualHost 配置中,您唯一需要做出的选择是重定向的永久或临时状态:

<VirtualHost *:80>
   Servername domain1.com
   RedirectTemp /page1 http://domain2.com/new-page-1
   RedirectPermanent /welcomepage http://domain2.com/new-welcome-page
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

回应上面的编辑#1:


当您在 ServerName 或 ServerAlias 中使用多个具有相同域名的 VirtualHost 节时,只有第一个是有效的,后续的将被忽略。

单个 VirtualHost 节可​​以包含多个 Redirect 指令,因此将第二个 Redirect 指令移动到第一个 virtualhost 节并删除第二个。

第二次阅读上面链接中的手册 确实有帮助

任何以 URL-path 开头的请求都会向目标 URL 所在位置的客户端返回一个重定向请求。超出匹配的 URL-path 的其他路径信息将附加到目标 URL。
示例: Redirect /service http://foo2.example.com/service
如果客户端请求http://example.com/service/foo.txt,它会被告知访问http://foo2.example.com/service/foo.txt

这与您对 www.domain1.com/AboutUs/Founders 的请求所观察到的完全一致,该请求触发RedirectPermanent / http://www.domain2.com/page12345/将原始请求重定向到 www.domain2.com/page12345/AboutUs/Founders

您可以通过正确排序重定向行来解决该问题,因为 Apache 将按顺序处理重定向指令。从最长的 URL 路径开始,否则它们会被较短目录上的有效重定向捕获。

<VirtualHost *:80>
   Servername domain1.com
   Redirect /AboutUs/Founders http://www.domain2.com/about-us-founders/
   Redirect /AboutUs/         http://www.domain2.com/about-us/
   Redirect /index.html       http://www.domain2.com/page12345/
   RedirectMatch ^            http://www.domain2.com/page12345/
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

重定向是只包括请求http://domain1.com您使用^ 而不是/通常这是一个好主意,也明确重定向IndexDocument为好,因此/index.html条目。