在Apache中基于域的redirect

我有一个.htaccess文件,在多个网站之间共享。 这些站点中的每一个都由同一组文件提供服务,但基于它们所运行的域名加载不同的configuration。

但是,我需要添加一些301redirect,我遇到了一个小问题。 我只想redirect执行一个特定的域,而不是每个.htaccess使用的域。 例如,我想redirectmysite1.com/some/pathredirect到mysite1.com/some/path ,但问题是,如果/some/path也存在mysite3.com它也redirect。

我基本需要的东西是这样的:

 <if host = mysite1.com> Redirect /some/path http://mysite2/some/other/path </if> 

这是可能的.htaccess中?

这是可能的 ,通过添加一个条件到你的RewriteRule例如。

 RewriteEngine on RewriteCond %{HTTP_HOST} ^www.othersite1.com$ [OR,NC] RewriteCond %{HTTP_HOST} ^www.othersite2.com$ [NC] RewriteRule ^some/path http://mysite2/some/other/path [R] 

这是做什么的:

  • 然后它testing请求的URL是否匹配“some / path”。 请记住,在.htaccess上下文中,URL与您的.htaccess文件所在的目录相关。如果这与上面的条件相匹配,则会进行评估。
  • 由于这两个条件是在一起的,所以只有一个需要匹配。 在这个例子中,我有两个我想要这个规则redirect的域。 您可以轻松地添加更多。
  • 最后URL被重写,并被redirect…