Htaccess redirect all traffic to new domain

To redirect all traffic from an old domain to a new domain using an .htaccess file, you can use the following code:

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

Let me explain what each line does:

  1. RewriteEngine On: Enables the rewrite engine.
  2. RewriteCond %{HTTP_HOST}!^newdomain.com [NC]: This condition checks if the HTTP host (i.e., the domain name) is not equal to newdomain.com (case-insensitive). The ! symbol negates the condition, so this rule will match if the host is not newdomain.com.
  3. RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]: This rule redirects any request that matches the condition to the new domain. The ^ symbol matches the start of the URL, and the .* matches any characters (including none). The $1 refers to the captured group (i.e., the part of the URL that matches the .* pattern). The http://newdomain.com/ part specifies the new URL, and the [R=301,L] flags indicate a permanent redirect (301) and that this is the last rule to be applied.

To use this code, simply replace newdomain.com with your actual new domain name, and upload the .htaccess file to the root directory of your old domain.

Note: