To redirect traffic from one domain to another using .htaccess, you will primarily use the mod_rewrite module. This method preserves the path of the original request, so olddomain.com/page correctly redirects to newdomain.com/page.
The Standard mod_rewrite Method
This is the most common approach for redirecting an entire domain while preserving the URL structure. Place this code in the .htaccess file in the root directory of the old domain.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$ [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L]
How it works?
RewriteEngine On: Activates the rewrite engine.
RewriteCond: Checks if the requested host is your old domain (with or without www). The [OR] flag allows either condition to match, and [NC] makes it case-insensitive.
RewriteRule: Captures the entire path ((.*)) and appends it to the new domain ($1). The flags mean: R=301 (permanent redirect, best for SEO), and L (stop processing further rules).
A Simpler Alternative: Redirect Directive
If you don not need the complex condition matching of mod_rewrite, Apache is built-in Redirect directive (from mod_alias) is a cleaner option for a simple domain move.
Redirect permanent / https://www.newdomain.com/
This tells Apache to permanently redirect all requests from the current domain is root to the new domain, automatically preserving the path and query string . Note that Redirect is typically placed in the server configuration, but it can work in .htaccess if the server allows it.