【问题标题】:How to redirect directory to another domain with Apache?如何使用 Apache 将目录重定向到另一个域?
【发布时间】:2012-05-03 01:22:15
【问题描述】:

我有一个 Apache 服务器,上面有两个域(通过 VirtualHost)。
文件设置如下:/example1/example2/
example1www.example1.com 的根
example2www.example2.com 的根
我想让www.example1.com/example2/<whatever else> 重定向到www.example2.com/<whatever else>
我可以访问主要的 Apache 配置文件。关于如何做到这一点的任何提示?我对配置 Apache 不是很熟悉,因此非常感谢任何解释。

与此相关的其他问题缺乏解释,所以我觉得我必须发布自己的问题。

【问题讨论】:

    标签: apache redirect


    【解决方案1】:

    您可以使用 .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 对重写的URL 使用Location 标头。 =301 告诉 Apache 使用 301 永久重定向标头。

    【讨论】:

    • 如果没有必要,我宁愿不使用 .htaccess 文件,那么如何将它放在主配置文件中呢?另外,这是如何工作的?
    • 好吧,经过一番研究,RewriteEngine 等应该在&lt;Directory "/example1/example2/"&gt; 中。此外,正则表达式无效。 ? 把它扔掉。就像现在一样,我只能获得 example2.com 的重定向循环
    • 无限循环?您是否将其添加到两个虚拟主机中?甚至是 example2 vhost?
    • 啊,要么我没有注意到&lt;VirtualHost&gt; 中的&lt;Directory&gt;,要么是我看到后添加的。好吧,我明白了,因为你指出了我正确的方向。我没有将&lt;Directory&gt; 放在&lt;VirtualHost&gt; 中,而是使用RewriteCond %{HTTP_HOST} ^(www\.)?example1.com,但我确信将它放在&lt;VirtualHost&gt; 中效果很好。
    • 是的,我在之后添加了它。 ;) 很高兴我能帮上忙!
    【解决方案2】:

    如果你有mod_alias 激活,你可以使用Redirect 指令:

    <VirtualHost *:80>
        ServerName example1.com
        Redirect 301 /example2 http://www.example2.com
    </VirtualHost>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-14
      • 2019-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-08
      • 2014-06-26
      • 1970-01-01
      相关资源
      最近更新 更多