Many developers assume that redirecting from one HTTPS domain to another is trivial, but there are critical technical and security considerations. The primary mechanisms are:
// Apache .htaccess example
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://example2.com/$1 [R=301,L]
// Nginx configuration
server {
listen 443 ssl;
server_name example.com;
return 301 https://example2.com$request_uri;
}
Modern browsers implement strict security policies for HTTPS redirects:
- Mixed Content warnings may appear if the SSL certificate chain isn't properly configured
- HSTS preload lists can make redirects irreversible
- Browser vendors may flag frequent cross-origin HTTPS redirects as suspicious
Valid use cases include:
// PHP header redirect (ensure no output before headers)
if ($_SERVER['HTTP_HOST'] == 'example.com') {
header("Location: https://example2.com" . $_SERVER['REQUEST_URI'], true, 301);
exit;
}
For permanent domain changes, consider:
// CNAME record in DNS
example.com. IN CNAME example2.com.
Each redirect adds latency. Measure with:
curl -w '%{time_redirect}\n' -o /dev/null -s https://example.com
Instead of redirects, you might want:
<link rel="canonical" href="https://example2.com/" />
When implementing HTTPS-to-HTTPS redirection between different domains (e.g., from https://www.example.com
to https://www.example2.com
), developers face unique technical hurdles that don't exist with HTTP redirects. The encryption layer introduces certificate validation complexities that must be handled properly.
Modern browsers implement strict security policies for HTTPS connections. When encountering a redirect to another HTTPS domain:
- Chrome performs full certificate chain validation for the target domain
- Firefox checks OCSP stapling during the redirect flow
- Safari enforces strict transport security (HSTS) requirements
Here are three common methods with their trade-offs:
1. HTTP 301 Redirect (Recommended)
# Apache .htaccess
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule ^(.*)$ https://www.example2.com/$1 [R=301,L]
2. Proxy-Pass (When Header Preservation is Needed)
# Nginx configuration
server {
listen 443 ssl;
server_name www.example.com;
location / {
proxy_pass https://www.example2.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
3. JavaScript Redirect (Fallback Option)
<script>
if (window.location.protocol === "https:") {
window.location.href = "https://www.example2.com" + window.location.pathname;
}
</script>
The main security implications include:
- Certificate warnings if the new domain's cert is invalid/expired
- HSTS preload conflicts if either domain is in browser preload lists
- Referrer header leakage between domains
For high-traffic sites:
- Implement the redirect at the load balancer level when possible
- Use HTTP/2 Server Push for associated resources on the new domain
- Set proper cache-control headers for the redirect response
Essential checks post-implementation:
# Verify redirect chain
curl -v -L https://www.example.com
# Check certificate validity
openssl s_client -connect example2.com:443 -servername example2.com | openssl x509 -noout -dates