将子域重定向到域并将所有内容保留在URL之后

时间:2016-02-27 00:06:18

标签: apache .htaccess mod-rewrite url-redirection

我需要重定向我的子域

  

http://sub.domain.com

  

http://domainB.com/news

我在没有任何结果的情况下测试了htaccess中的各种可能性。

此代码工作正常,但是从域重定向,而不是从子域重定向...

RewriteCond %{HTTP_HOST} ^domain.com$ 
RewriteRule ^(.*) https://subdomain.domainB.com/$1 [P]

此代码将domain.com重定向到

  

https://subdomain.domainB.com

并在地址栏中保留domain.com并正常运行。我希望子域名为subdomainB。

我需要帮助。

提前致谢;)

2 个答案:

答案 0 :(得分:1)

您需要在RewriteCond中与子域匹配。

RewriteCond %{HTTP_HOST} ^sub\.domain\.com$ 
RewriteRule ^(.*)$ https://subdomain.domainB.com/$1 [P]

答案 1 :(得分:1)

除了starkeen的答案之外,在将SSLProxyEngine On用于Apache设置时,您应该添加 SSLEngine on ,否则会引发内部服务器错误(500):

SSLEngine on
SSLProxyEngine On
SSLCertificateFile    /etc/ssl/XXXXX_webserver_ssl.crt
SSLCertificateKeyFile /etc/ssl/XXXXX_webserver_ssl.key

RewriteCond %{HTTP_HOST} ^sub\.domainA\.com$ 
RewriteRule ^(.*)$ https://subdomain.domainB.com/$1 [P,L]