将非 www 重定向到 www,不包括子域

Redirect non-www to www excluding subdomain

首先,请不要将此问题标记为重复,因为我已经尝试了所有其他答案,但没有人适合我。我有一个名为 account.domain.com 的子域,这是我 .htaccess:

中的代码
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

我想将访问从 domain.com 重定向到 www.domain.com,这很好用。当我访问子域 account.subdomain.com 并重定向到 www.account.subdomain.com/account/.php 时出现问题...我不知道为什么。

我测试了很多不同的代码,但我得到了相同的结果。我的网站托管在 Hostinger.es.

的免费帐户中

如果您只想定位主域,请使用:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]