Redirect all URLs to another domain with .htaccess

You changed your domain name and you want to redirect all requests to the new domain? You can create such a redirect by adding a few lines in the site’s .htaccess file.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ "https://newdomain.com/$1" [R=301,L]

To redirect with a 301 HTTP response ( Moved Permanently ), use R=[301,L] on the last line.

To redirect with a 302 HTTP response ( Moved Temporarily ), use R=[302,L] on the last line.

The L indicates that this is the last rule from the .htaccess file that will be processed.

301 Moved Permanently
The requested resource has been assigned a new permanent URL and
any future references to this resource should be done using that
URL.

RFC 1945

302 Moved Temporarily
The requested resource resides temporarily under a different URL.

RFC 1945

Resources:
RFC 1945