Apache VirtualHost重定向,仅更改域

时间:2018-08-08 10:04:40

标签: apache url-redirection virtualhost

如何创建VirtualHost来重定向仅更改其域(包括子域和参数)的所有链接:

exampleA.com -> exampleB.com
test.exampleA.com -> test.exampleB.com
test1234.exampleA.com/url/test.html?param=222 -> test1234.exampleB.com/url/test.html?param=222

我想重定向*等所有子域,它应该是永久301

现在我有一个简单的301重定向

<VirtualHost *:80 *:443>
        ServerName exampleA.com
        ServerAlias *.exampleA.com

        RewriteEngine On
        Redirect 301 / https://exampleB.com
</VirtualHost>

2 个答案:

答案 0 :(得分:2)

我从未做过这样的事情,但是尝试在您的virtualhost文件中使用redirect选项。首先启用重写

sudo a2enmod rewrite

然后在您的virtualhost文件中

RewriteEngine on
RewriteCond %{SERVER_NAME} =exampleA.com [OR]
RewriteCond %{SERVER_NAME} =www.exampleA.com
RewriteRule ^ https://exampleB%{REQUEST_URI} [END,NE,R=permanent]

在此处详细了解此内容:https://httpd.apache.org/docs/2.4/rewrite/remapping.html

它将采用原始域并重写为另一个域。我记得在旧版本的apache中是这样的:

<VirtualHost *:80>
  ServerName www.domain1.com
  Redirect / http://www.domain2.com
</VirtualHost>

答案 1 :(得分:2)

我找到了解决方法:

<VirtualHost *:80 *:443>
        ServerName exampleA.com
        ServerAlias *.exampleA.com

        RewriteEngine On
        RewriteCond %{HTTP_HOST} (.+\.)?exampleA\.com$ [NC]
        RewriteRule (.*) https://%1exampleB.com$1 [R=301,L]
</VirtualHost>