简体   繁体   English

添加 301 重定向以将所有流量从一个域重定向到另一个域

[英]Add a 301 redirect to redirect all traffic from one domain to another domain

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.我正在尝试使用 .htaccess 将所有流量从旧域(具有多个子域和 URL)重定向到新域。 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]

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.一般来说,此类规则最好在中央 http 服务器的主机配置中实施。 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").如果您无权访问它(阅读:如果您使用廉价的托管服务提供商),您可以使用分布式配置文件(通常称为“.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.在这种情况下,您需要启用该功能(请参阅 http 服务器文档中的AllowOverride指令),并且您需要将该文件放在 http 主机DOCUMENT_ROOT文件夹中,并注意它可以被 Z80791B3AE78CZ9FAACB88 服务器读取。

And a side note: in your implementation you redirect to the unencrypted http protocol.附带说明:在您的实现中,您重定向到未加密的 http 协议。 That really is outdated these days, you really should use the encrypted https protocol.这几天真的过时了,你真的应该使用加密的 https 协议。 Sure, you need a valid certificate for that, but those are free these days.当然,你需要一个有效的证书,但这些天是免费的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM