When migrating from xy.example.com
to abc.example.com
, we need to implement a permanent redirect (HTTP 301) that preserves the entire request path. This means:
http://xy.example.com/team.php → http://abc.example.com/team.php http://xy.example.com/blog/post → http://abc.example.com/blog/post
There are two primary methods to achieve this in Apache:
Method 1: Virtual Host Configuration
The most efficient approach is to configure this at the virtual host level:
<VirtualHost *:80>
ServerName xy.example.com
Redirect permanent / http://abc.example.com/
</VirtualHost>
Key points:
Redirect permanent
sends a 301 status code (better for SEO than temporary redirects)- The trailing slash after the domain ensures path preservation
Method 2: Using .htaccess
For situations where you can't modify the main config:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^xy\.example\.com$ [NC]
RewriteRule ^(.*)$ http://abc.example.com/$1 [L,R=301]
After implementing, verify with:
curl -I http://xy.example.com/team.php
You should see:
HTTP/1.1 301 Moved Permanently Location: http://abc.example.com/team.php
For HTTPS redirects:
<VirtualHost *:443>
ServerName xy.example.com
SSLEngine on
# SSL certificates here
Redirect permanent / https://abc.example.com/
</VirtualHost>
To handle both www and non-www versions:
RewriteCond %{HTTP_HOST} ^(www\.)?xy\.example\.com$ [NC]
RewriteRule ^(.*)$ http://abc.example.com/$1 [L,R=301]
When migrating from an old domain (xy.example.com
) to a new one (abc.example.com
), it's crucial to maintain URL structure integrity. This means:
- Root domain requests should redirect properly:
xy.example.com
→abc.example.com
- Path preservation:
xy.example.com/team.php
→abc.example.com/team.php
- Query parameters should carry over:
xy.example.com/search?q=test
→abc.example.com/search?q=test
Option 1: Using VirtualHost with RewriteEngine
<VirtualHost *:80>
ServerName xy.example.com
ServerAlias www.xy.example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?xy\.example\.com [NC]
RewriteRule ^(.*)$ http://abc.example.com$1 [L,R=301]
</VirtualHost>
Option 2: Using Redirect Directive
<VirtualHost *:80>
ServerName xy.example.com
Redirect permanent / http://abc.example.com/
</VirtualHost>
Key differences between these approaches:
Method | Path Preservation | HTTP Status | Flexibility |
---|---|---|---|
RewriteRule | Yes | 301 | High (regex support) |
Redirect | Only root | 301 | Low (simple cases) |
After implementing the redirect:
- Restart Apache:
sudo systemctl restart apache2
- Test with curl:
curl -I http://xy.example.com/team.php
Expected response headers:
HTTP/1.1 301 Moved Permanently Location: http://abc.example.com/team.php
For complex redirect needs, consider these additional parameters:
RewriteRule ^(.*)$ https://abc.example.com$1 [L,R=301,NE,QSA]
Where:
NE
: No escaping of special charactersQSA
: Query string appendR=301
: Permanent redirect (SEO-friendly)
For HTTPS redirection, modify the target protocol:
RewriteRule ^(.*)$ https://abc.example.com$1 [L,R=301]
Or configure separate VirtualHost for port 443 with SSL certificates.