将非 www 重定向到 www,导致循环地址

redirect non-www to www resulting in loop address

我正在配置我的网站以将任何非 www 重定向到 www,但会导致这样的地址循环:

abc.com/www.abc.com/index.php/www.abc.com/index.php/www.abc.com/index.php/www.abc.com/index.php/www.abc.com/index.php ... and so on

下面是我的 .htaccess 文件。谁能帮我找出我的错误?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/assets|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)\.html$ /index.php/ [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(/index\.php|/assets|/robots\.txt|/favicon\.ico)
RewriteRule ^(.*)$ /index.php/ [L]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ www.%{HTTP_HOST}/index.php/ [R=301,L]
</IfModule>

<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>

非常感谢,

RewriteRule ^(.*)$ www.%{HTTP_HOST}/index.php/ [R=301,L]
                   ^---

您忘记在主机名前加上前缀 http://,所以 Apache 重写为 LOCAL url

尝试

RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/index.php/ [R=301,L]

相反。

基本相同的规则适用于:

<img src="www.example.com/kittens.jpg" />
<img src="http://www.example.com/kittens.jpg" />