Apache redirect from one subdomain to another

2020-05-05 17:39发布

问题:

Does anyone know a way to do a permanent redirect from a.example.com to b.example.com? I have other subdomains that need to remain as they are though. so:

first.example.com -> second.example.com
third.example.com is fine
fourth.example.com is fine 

I want to do this in the <VirtualHost> block rather than an .htaccess to avoid having to redeploy again.

Any thoughts would be appreciated, thanks

回答1:

Add the following RewriteRule to your VirtualHost

RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.example.com$
RewriteRule ^ http://second.example.com [R=301,L]

If you wanted to redirect first.example.com/some/url to second.example.com/some/url:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.example.com$
RewriteRule /(.*) http://second.example.com/$1 [R=301,L]

Recommend you use [R=302] whilst testing the rules to avoid problems with 301s getting cached by your browser.