.htaccess 以及如何正确设置 RewriteEngine

.htaccess and how to set RewriteEngine correctly

我拥有这 4 个域:

-example.it
-www.example.it
-example.com
-www.example.com

我希望他们全部 301 重定向到 www.example.com。我目前 运行 Apache2 Ubuntu。

我的 .htaccess 文件的正确语法是什么?

谢谢:)

您可以在 htaccess 文件中使用以下重写规则:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(example\.com|example\.it)$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [NE,L,R=301]

简短说明:

第一个 RewriteCond 排除目标域 www.example.com 我们已经排除它以避免重定向循环错误。第二个条件匹配 www.example.itexample.comexample.it 然后规则将请求重定向到 www.example.com .