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:
RewriteEngine On
: Enables the rewrite engine.RewriteCond %{HTTP_HOST}!^newdomain.com [NC]
: This condition checks if the HTTP host (i.e., the domain name) is not equal tonewdomain.com
(case-insensitive). The!
symbol negates the condition, so this rule will match if the host is notnewdomain.com
.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). Thehttp://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:
- Make sure to test the redirect in a browser or using a tool like
curl
to ensure it's working as expected. - If you have other rewrite rules in your
.htaccess
file, you may need to adjust the order of the rules to ensure this redirect is applied last. - If you're using a CMS or framework, you may need to update the configuration files to point to the new domain.
- Be aware that this redirect will only work for HTTP requests. If you want to redirect HTTPS traffic as well, you'll need to add additional rules.