Home > Back-end >  Add a 301 redirect to redirect all traffic from one domain to another domain
Add a 301 redirect to redirect all traffic from one domain to another domain

Time:09-04

Could anyone help?

I'm trying to redirect all traffic from an old domain (with multiple subdomains and URL) to a new domain using .htaccess . I just want to redirect traffic from all existing links (old domain) to the English subdirectory of the new domain.

I would like to know if this line is correct?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule ^(.*)$ http://newdomain.com/en [R=301,L]

CodePudding user response:

The rule you implemented is "correct" in that it should work if some preconditions are met ...

I would simplify it a bit, though:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(\w \.)?olddomain\.com$
RewriteRule ^ https://newdomain.com/en [R=301,L]

In case the old and the new domain are served from separate locations or even separate systems you can further simplify that by leaving away the condition:

RewriteEngine On
RewriteRule ^ https://newdomain.com/en [R=301,L]

In general such rules are best implemented in the central http server's host configuration. If you do not have access to that (read: if you are using a cheap hosting provider) you can instead use a distributed configuration file (often called ".htaccess"). In that case you need to enable that feature (see the AllowOverride directive in the documentation of the http server) and you need to place that file in the http hosts DOCUMENT_ROOT folder and take care that it is readable by the http server.

And a side note: in your implementation you redirect to the unencrypted http protocol. That really is outdated these days, you really should use the encrypted https protocol. Sure, you need a valid certificate for that, but those are free these days.

  • Related