How to Implement Complete Domain Redirection with Apache VirtualHost and mod_rewrite


2 views

When implementing domain redirection from test1.com to www.test.com, several technical considerations emerge. The primary issue isn't just basic redirection, but handling all possible request variations including:

  • Naked domain (test1.com)
  • WWW prefix (www.test1.com)
  • Subdomains (dev.test1.com, blog.test1.com)
  • HTTP/HTTPS protocols

The initial approach using mod_rewrite works for most cases but fails for the naked domain. Here's why:



    ServerName test1.com
    ServerAlias www.test1.com *.test1.com
    RedirectPermanent / http://www.test.com/

Key improvements from the original attempt:

  • Using RedirectPermanent (HTTP 301) instead of mod_rewrite for simplicity
  • Explicitly listing both naked and www versions in ServerAlias
  • Wildcard (*.test1.com) covers all subdomains

For more control over the redirection process, consider these alternatives:

Option 1: mod_rewrite with Comprehensive Matching



    ServerName test1.com
    ServerAlias www.test1.com *.test1.com
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www\.)?test1\.com$ [NC,OR]
    RewriteCond %{HTTP_HOST} ^(.+)\.test1\.com$ [NC]
    RewriteRule ^(.*)$ http://www.test.com/$1 [R=301,L]

Option 2: Separate VirtualHosts for Naked Domain



    ServerName test1.com
    RedirectPermanent / http://www.test.com/



    ServerName www.test1.com
    ServerAlias *.test1.com
    RedirectPermanent / http://www.test.com/

When implementing domain redirection, watch out for these issues:

SSL/TLS Considerations

If test1.com has SSL certificates, you'll need to handle HTTPS redirection:



    ServerName test1.com
    ServerAlias www.test1.com *.test1.com
    SSLEngine on
    # SSL certificate configuration here
    RedirectPermanent / https://www.test.com/

Path Preservation

To ensure all URIs redirect properly while maintaining paths:


RedirectMatch 301 ^/(.*)$ http://www.test.com/$1

While all these solutions work, they have different performance characteristics:

  • Redirect directive: Fastest, simplest, but least flexible
  • mod_rewrite: More flexible but slightly heavier
  • Multiple VirtualHosts: Cleanest separation but requires more configuration

Remember that proper DNS setup is crucial for this to work:

  • All test1.com variants should point to Server 2's IP
  • TTL values should be adjusted before making DNS changes
  • Consider implementing a CNAME for subdomains if appropriate

After implementation, verify with:


curl -I http://test1.com
curl -I http://www.test1.com
curl -I http://sub.test1.com/path

All should return 301 status with Location: http://www.test.com/...


When attempting to redirect all traffic from www.test1.com (and its variations) to www.test.com, many administrators encounter incomplete redirection - particularly with naked domains (test1.com without www). This occurs because the VirtualHost configuration isn't properly catching all domain variations.

The most reliable approach combines ServerAlias with either mod_rewrite or Redirect directives:


    ServerName test1.com
    ServerAlias www.test1.com *.test1.com
    Redirect permanent / http://www.test.com/

The key elements that make this effective:

  • ServerName catches the naked domain (test1.com)
  • ServerAlias handles www-prefixed and all subdomains
  • Redirect provides a simple 301 permanent redirect

For more complex redirection needs, mod_rewrite offers additional control:


    ServerName test1.com
    ServerAlias www.test1.com *.test1.com
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\.test\.com$ [NC]
    RewriteRule ^(.*)$ http://www.test.com/$1 [R=301,L]

1. Missing DNS Records: Ensure both A records (test1.com and www.test1.com) point to Server 2's IP.

2. SSL Redirection: For HTTPS sites, include matching VirtualHost blocks on port 443 with SSL certificates.

3. Infinite Loops: Always test redirects to verify they don't create redirection cycles.

While both methods work, the Redirect directive is slightly more efficient than mod_rewrite for simple domain-to-domain redirects. However, mod_rewrite provides more flexibility for:

  • Preserving URL paths
  • Conditional redirects based on request attributes
  • Complex pattern matching

Here's a production-ready configuration that handles both HTTP and HTTPS:

# HTTP Redirect

    ServerName test1.com
    ServerAlias www.test1.com *.test1.com
    Redirect permanent / https://www.test.com/


# HTTPS Redirect (if SSL is configured)

    ServerName test1.com
    ServerAlias www.test1.com *.test1.com
    
    SSLEngine on
    # SSL certificate paths here
    
    RewriteEngine On
    RewriteRule ^(.*)$ https://www.test.com/$1 [R=301,L]