codeigniter将www添加到htaccess中的url正在停止POST请求工作

I have encountered a rather strange issue while adding www to the urls in .htaccess. I have a codeigniter based site and adding www to all urls. But my Post request have stopped working. Here is the content of my apache .htaccess file

RewriteEngine on
#for adding www
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

RewriteCond $1 !^(index\.php|img|public) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]

I am using $config['uri_protocol'] = 'AUTO';

and it does work fine with non www urls but it stops POST requests working after adding www as stated in above. I have even tried REQUEST_URI, but it didn't help. some other settings i have are.

$config['base_url'] = 'http://example.com';

and in autoload $autoload['helper'] = array('url');

I guess the problem is that the 302 redirect after adding www does not understand POST data.

Instead of having the 301 redirect within .htaccess, just bake that logic into your app.

For example on every request you could do:

if (strstr($_SERVER['HTTP_HOST'], 'www') === FALSE) {

    $domain_with_www = 'http://www.example.com';
    header ('HTTP/1.1 301 Moved Permanently');
    header ("location: ".$domain_with_www.$this->uri->uri_string);
}

You can get this to run on every request by extending CI_Controller. See this for more info on that: http://codeigniter.tv/a-10/Extending-the-core-MY_Controller-and-beyond

Change your base URL from:

http://example.com

to:

http://www.example.com